While we are on the subject of dates. Another date question

This goes deeper into the date thing. I want to be able to check the date and time stamp in or on a file to see what the time span is.

We have a job that runs several times an hour - kicked off through cron based on a trigger file. We want to keep track of each run and check the time between runs for reasonbleness because the job can hang and not notify anyone. Right now we have someone watching the job during peak hours and manually determining that it has been in the run state too long. I would like to automate the reasonbleness check for automated notifies.
(the hanging is out of our control so we need to automate the monitor process)
We put a time stamp in a log of when the job starts, so we already have that peice. What I don't know how to do is manipulate date and time down to minutes.

THANKS!

Check the man page for the `date` command.
Most of the time, it will follow a format similar to the following:
date +%H:%M:%S - This will give a format of the following:
07:09:59

But the most help you'll probably get is:
man date

thank you , I guess I should have been more specific. I know how to get the date and time.
I need to obtain the time difference between the start times of my jobs.
ie. the job kicks off at 8:30:00 then again at 8:47:00 I need to calculate the difference between these two times. then I can do a reasonbility check for the time span - if the time span is greater than 15mins, I want to send a notify to a pager. I know how to send the notify, what I am looking for is how to determine the time span / difference.

thanks

why not toss another date time stamp w/ something signifiing then end of the run. This will alow you to do 2 things.

1) let you know how long it took to do the job in the first place.
2) let you know the time between jobs.

ie:

==== Job Begins date-time-stamp ====
what ever you do to your files
......
==== Job EOF date-time-stamp ====

I have that information.
I am not looking for logical approaches....
I am looking for the technical way of how do I subtract the start time from the end time to get the time span?

I have the time 08:30:00 how do I write script code to subtract it from 08:47:00 ???????????

I can awk out the hour and minutes and seconds and work that way then put it all back together, but I want to know if anyone has an easier way to calculate elapsed time.

Well, you can take the hour, multiply by 60, then add the minutes to get minutes after midnight. But how on earth does comparing the start times of two jobs help you know when one is hung? And why worry about the start times of jobs started via cron anyway?

If want to detect a job that runs longer than 15 minutes, just use a shell wrapper:

#!/usr/bin/ksh
job_that_might_hang &
pid=$!
sleep 900
if kill -0 $pid 2>/dev/null ; then
   echo background job is still running
else
   echo background job finished
fi
exit 0

do something like this:

#start of job
echo $(date +%H%M%S) >> /some/place/log
#
# job here
#
echo $(date +%H%M%S) >> /some/place/log
#now, to compare
date_1=$(tail -2 /some/place/log | head -1)
date_2=$(tail -1 /some/place/log)
check_time=$(expr ${date_2} - ${date_1})
if [ "$check_time" -gt "1500" ] ; then
     echo "Uh oh!" | mail mizzgail -s "your process got stuck"
fi

This isn't very proactive, but it would be fairly easy to construct a script that could poll, comparing the current time (in a similar fasion as above) to the start time. Also, instead of mail, you could send a message directly to a users console... etc...

Hope that helps.

To get into the nitty gritty of the "whys"...
Our job is scheduled via cron in a way, but actually runs based on a trigger file. The cron checks for certain conditions before it launches a session inside an ERP system. The job runs inside an ERP application and can hang at any given point and just not go anywhere, which means that everything appears to be "running" but its really not doing anything. The script itself will not end because it needs the application to end, which its not going to do. (no fix in sight so we have to deal with this)
There is really no way to deal with this except a reasonability check to see if the job has been "running" too long and notify support. There are some points along the process that are outside our control. (too much to go into).
I know this sounds hokey, but its the best we can do for this.
I'll try the different approaches for the time and see if they work for us. The "peak" hours that we are concerned about do not overlap days. That part is easy.

now that you have explained it a bit more. your looking for a realtime action to happen.

If your email funtion is at the base of the script but the script is stuck running in a hung state if you will; your script will never get to the notification funtion.

one line at a time till the end of the file.

A point brought up by my coworker. is.

After the file gets proccessed by your aplication does it dump back to disk? Does it disapear? what happens if an error happens?

You could set up a script that will check for files that have completed in x dir. if you dont see x file send an email. or vise versa.

any thoughts anyone?

Take another look at the script that I posted. It launches the real program in the background, and after 15 minutes, it checks to see if the background job is still running. This will reliably detect any instance of the background job running past the 15 minute deadline.

Yes. I caught that the job runs in the background on that script. Good idea - we need to see if we can do that with the application.
There is no output from the session as it is processing data into the ERP system to book sales orders, but that could also be an option.
We don't have consoles, we connect off PC workstations, thus, we mail ourselves the job status to our internet mail id. If its extremely urgent, we send a message to the support pager. During identified critical hours, this is one of the ones that will get sent to the pager.
Thanks for all the ideas.. keep them coming if you have any more.

Hi you,

I think my problem is very similar, so I posted my question here.
I want to measure the time my script runs. So that at the end of teh script I can tell the user the execution of the script demo took 11min 05 sec.

Now I stored the date of the execution and the endtime of the execution.
I just calculated the time like LivinFree in his script.

But the problem is, that if the script starts at 10:59:00 and ends at 11:01:00
I get as result a time of 42 minutes!!!

How can I solve this??

Or is there a better way to measure the time a acript takes within a script. I don't like to have two scripts, with one in the background...

THX

Ben Sky

If you are using ksh you can just use the SECONDS variable. It is the number of seconds that ksh has been running.

THX I did not know, that this variable exists, but I found it in the man page of ksh!! My fault....

And is there an easy way to convert than the seconds into minutes and secondes or just the way of calculating by dividing with 60?!

THX
Ben Sky

There is a time command that will feed back elapsed and system times.
The format to use in a script would be like:
(time command ) > filename.time 2>&1

You will see output like
real 10.5
user 0.3
sys 3.6

in the filename.time file...

check the man pages for time command.