shell script for sleeping 1 week - unix

Can anyone give me the script for 'sleep a job for entire week' as a shell command.
Before sleeping in every run it should calculate the next week's date and time.
I found something which was bit useful
startTime=$(date +%s)
endTime=$(date -d "next week" +%s)
timeToWait=$(($endTime- $startTime)) sleep $timeToWait

date -d "next week"
is a different way to say
date -d "7 days"
This means "seven days from now", which is not the start of the next week and is also probably not what you want. If it is what you want and you are using GNU sleep, you can just issue this command:
sleep 7d
to have your script sleep for seven days. If you are not using GNU sleep, then you will have to convert the seven day interval to seconds:
sleep 604800
If you want the start of the next week, then this might be of help in ksh, or even in bash:
let time=`date -d "next monday" +%s`-`date +%s`
sleep $time
That said, I think that you are using the wrong tool for this job. Having a script sleep for seven days is quite impractical - any number of things could happen in the mean time, from the system rebooting to the shell process being killed.
You should look at crond and crontab, atd and at or even at a more modern cron variant/supplement, such as frcon or anacron.

May I ask why do you require a 1 week sleep?
I ask because if you want a process to wait for a week and then retry/ do something, cron job may be better. For 2 reasons -->
1) Save resources
2) If for some reason your system has to be restarted/ goes down - your shell script will stop and whatever you were waiting for will not happen.
Whereas a cron job will run exactly when you want it to run saving you from such a scenario. Some useful examples are in the link below
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
I learned of this because I experienced it myself! :)

coreutils sleep supports the d postfix so:
sleep 7d
Will sleep 7 days.

I warned against a single long wait, and I would not do this with a waittime of 1 week.
But I have used variations of this in the past:
waittime=
interval=
: ${waittime:?specify wait time in seconds}
: ${interval:?specify interval in seconds}
t0=SECONDS
target=$(( t0 + waittime ))
until
(( SECONDS > target ))
do
sleep $interval
done

Related

Date for the next run of the job in Autosys

I have a job which runs on business days (MON-FRI). I need the date for the next run of the job. so Suppose if Job is running on Monday I need date of Tuesday or if Job is running on Friday I need date of Monday.
The job_depends command will let you see the future schedule for a job. Use the -J parameter with the job name, the -t parameter to denote time dependency, and the -F parameter with a current datetime stamp to find the next run in the future.
You may use Job Forecast report by using -t and -F combination.
job_depends -t -w -J <JOB_NAME> -F "03/29/2017" -l0
Note that this will work only for Jobs which have Date Condition. You may use -l0 if only looking for top level box.
Job Forecast Report
From: 03/29/2017 00:00:00 To: Infinity
Job Name Next Start Atomic Start Conditions
______________________________ _______________ _______________________
JOB_NAME 03/29/2017 20:40:00 -------

Overrunning User Process Monitoring at Unix over a period of Time

I am new to Administration front. I have a requirement :
Identify the user processes (have a list of users who submits the process) which are still active and which have been submitted 3/4 days ago.
My Approach on this:
Have a text file with list of users.
Loop and find the current processes spawned by the users and store it a file.
Substitute a date variable with the format and grep.
However, I am stuck how to get : Submitted 3/4 days ago. With my code its equating to a day.
#!/bin/sh
rm -f psinfo.txt
rm -f psinfo_backdated.txt
for i in `cat user.lst `;
do
ps -ef | grep "$i" >> psinfo.txt
done
grep `date -d'2 day ago' +%b%d` psinfo.txt > psinfo_backdated.txt
I really need your comments and answer on this Gurus.
If some can tell if we can do grep of date range from a file like Less
than Apr27 format. I can make my script work. Waiting for the guru's
to respond on this.
A time format like Apr27 is not suitable for the task, also because it doesn't contain the year, which is necessary at the start of a year. Fortunately, there is a much better format for the start time of a process. Replace
ps -ef | grep "$i" >> psinfo.txt
with
ps -oetime=ELAPSED_TIME -ouser,pid,ppid,cpu,tty,time,args -u$i >>psinfo.txt
(you might want to drop fields you don't need from the second -o…). The time since start is then represented in the form
[[days-]hours:]minutes:seconds
- this you can easily filter with awk, e. g. to get processes started 3 or more days ago:
awk '{ if ($1~/-/ && int($1)>2) print }' psinfo.txt >psinfo_backdated.txt

while using script in unix how can I get the bell to chime more than once

I am writing a shell script and need the bell to chime several times. Is there a command variation or argument to make this happen ?
I have used the \a and the \007 and I get one chime. I can't seem to find how to make it happen more than once.
run your beep command once, wait a second with sleep and run it again
for instance
echo -n $'\a' ; sleep 1; echo -n $'\a'

Kill fbi frame buffer process, after x time

I have a problem with a sh script.
I am using a raspberry, and want in my script to display with fbi frame buffer an image for 10 seconds. After these 10 seconds I want my script to run other sequential commands.
I wrote:
[...]
if[...]
fbi --noverbose $MEDIAFILE
MYPID=pgrep fbi
echo "[$MYPID] this is MY PID - and now i kill it!!!"
[...]
but my script stops itself in the first line (fbi --noverbose...) and I can't kill it in the next line :(.
I can't execute the command in background because I need to see the image...
any idea? thank you!
If your goal is to not show anymore after a certain amount of seconds, you can also add the command line options "-t secs" and "-1". "-t secs" is used for slideshows and is the time after which the next image is shown and "-1" means that the slideshow wont loop.
In your case:
fbi --noverbose -t 10 -1 $MEDIAFILE
This shows the image for ten seconds and then the fbi command finishes. No need to kill the process.
If fbi can't be run in the background, put your kill command in the background. To make it happen after a delay, use a subshell that sleeps first, then runs the kill command. The script would look something like this:
( sleep 10 ; kill $(pgrep fbi) ) &
fbi somefile

How to tail -f a file (or similar) for a specified interval?

I am working on adding some nagios alerts to our system -- some of which will monitoring the rate of certain events hitting the nginx/apache logs (or parsing values from those logs.) The way I've approached the problem so far is with a simple shell script tail -f'ing the log for 25 seconds or so to a temporary file, killing the process, and then running awk, etc over the temp file. The goal here being to get a log "sample" over 25 seconds and then perform analysis.
This is less than ideal obviously because of the increase in disk IO due to these temp files -- what I really would like is an "enhanced" tail -f that would terminate the pipe cleanly after a certain number of seconds. Ie:
tail -f --interval '5 seconds' | grep "/serve"
Would tail the log for 5 seconds and show me all the lines that have "/serve".
I'd imagine I can whip up a ruby script to do this pretty quickly, but I wanted to make sure there wasn't a more unixy way to accomplish this. At a high level, is there a better way to be taking samples of a log from the last N seconds (and no, I'd rather not be parsing timestamps, etc.)
Found the solution. "apt-get install timeout" :)
Edit: Actually this kills tail, doesn't cause it to exit gracefully, so we lose the entire pipe. What I want to work is:
timeout -15 5 tail -f /mnt/log/nginx/nginx-access.log | grep '/javascripts' | wc -l
To tell me how many javascript files served in last 5 seconds, etc.
A slightly different approach:
(tail -f /var/log/messages & P=$! ; sleep 5; kill -9 $P) | grep /serve
I'm thinking that, as a Nagiios user myself, you do not want probe processes pausing for arbitrary amounts of time. That's going to, in the worst case, make Nagios check other things less often, or "clump" the checks.
What about a script that runs quickly (instantly) and parses the last few lines of the file, returning only interesting things with a timestamp later than a given time?
GNU's tail has a --pid flag that can be used for this (tail will exit once a process with that PID no longer exists). Just start up a sleep process in the background and tell tail to exit when it does. Like so:
sleep 5 & tail --pid=$! -f /var/log/system.log
tail will exit with a 0 exit code when time is out.

Resources