Operating System - HP-UX
1847799 Members
2523 Online
104021 Solutions
New Discussion

Re: Monitor script running time

 
Mike_316
Frequent Advisor

Monitor script running time

Hey Gang,

I need to develop a script which can monitor the time a script runs from start to finish. This particular project requires the script doing the monitoring run externally from the scripts it is monitoring, monitor multiple scripts simultaneously, get the timestamp down to the second and have as little overhead as possible.

My first thought was to have a background process grep a 'ps -ef' and look for specific indicators of a script running (like a script???s name.) When it finds a script running, grab a timestamp and then when the script is no longer there, get another timestamp and do a comparison. The problem I see (besides a lot of overhead) is if the script does a 'ps -ef' every second, and takes one second to calculate everything when it sees/stops seeing a process...there would be an up-to-4-second corruption on the actual time the script ran.

Is there a way to have the script monitor a process, or process ID, with more accuracy and less overhead? I can use UNIX shell scripting or Java is a possibility as well.

Thanks!

Mike


"If we treated each person we met as if they were carrying an unspeakable burden, we might treat each other as we should" - Dale Carnegie
5 REPLIES 5
Pete Randall
Outstanding Contributor

Re: Monitor script running time

Mike,

Have you looked into the time command?

Pete

Pete
Oleg Zieaev_1
Regular Advisor

Re: Monitor script running time

Mike
no need to develop.
You can use timex (1m).

Hope this helps,
0leg
Professionals will prevail ...
Tom Maloy
Respected Contributor

Re: Monitor script running time

My first thought would be to "wrap" each script that must be monitored.

Either use "time" to run the script and save those results, or run "date" before and after the script.

Tom
Carpe diem!
Sridhar Bhaskarla
Honored Contributor

Re: Monitor script running time

Hi Mike,

Along with "time" and "timex" as suggested above, there is an env. variable called SECONDS that tells you how long the shell has been active. So if you are developing a script, you can do something like this.

#!/usr/bin/ksh
START_TIME=$SECONDS
...
(Your code here)
...
END_TIME=$SECONDS
(( ELAPSED_TIME = END_TIME - START_TIME ))

echo elapsed time is $ELAPSED_TIME


-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Tom Danzig
Honored Contributor

Re: Monitor script running time

Mike,

If you are using the korn or posix shell, you can simpley check the value of $SECONDS at the end of the script. This is a built in variable that starts at zero when the script starts.

In the last line of your script, put:
echo "Script ran for $SECONDS seconds"


Tom