Minutes a program runs.

I have a ksh script that executes a program with a predetermined timeout in minutes. If the program takes longer then the timeout then it still completes with a return code of 0. :confused:

I would like to determine how long the program ran. Then if it takes longer than the timeout I would like to specify a new return code. :b:

I tried to use the time command to display to the screen and log file. Then grab the real time from the output in the log file however I have had not success with redirecting the output to the log file. :o

Here is what I am trying to do. See how the output of time does not show up in the log. :mad:

$ time ls home test 2>&1 | tee -a test.log
ls: 0653-341 The file home does not exist.
test

real 0m0.01s
user 0m0.00s
sys 0m0.02s
$ cat test.log
ls: 0653-341 The file home does not exist.
test

There is another problem with this method as well. Depending on what system I run the program on it could take more than an hour to run. Then the real line would return something similar to this �real 4h15m0.23s�. :o

Does anyone have a good idea of what I could to determine how long my program is running? Or help me with what I have already started? :slight_smile:

Hi.

I think there are 2 ways to accomplish this:

#!/bin/ksh

# @(#) s1       Demonstrate capture of timing data with ksh.

echo " pdksh version: $KSH_VERSION"

rm -f a.log b.log

echo
echo " Running with time."

{ time ls home test; } 2>&1 | tee -a a.log

echo
echo " Running with /usr/bin/time."

/usr/bin/time ls home test 2>&1 | tee -a b.log

echo
echo " Results of time in a.log:"
cat a.log

echo
echo " Results of /usr/bin/time in b.log:"
cat b.log

exit 0

Producing:

% ./s1
 pdksh version: @(#)PD KSH v5.2.14 99/07/13.2

 Running with time.
ls: home: No such file or directory
ls: test: No such file or directory
    0.00s real     0.00s user     0.00s system

 Running with /usr/bin/time.
ls: home: No such file or directory
ls: test: No such file or directory
Command exited with non-zero status 1
0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+177minor)pagefaults 0swaps

 Results of time in a.log:
ls: home: No such file or directory
ls: test: No such file or directory
    0.00s real     0.00s user     0.00s system

 Results of /usr/bin/time in b.log:
ls: home: No such file or directory
ls: test: No such file or directory
Command exited with non-zero status 1
0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+177minor)pagefaults 0swaps

This is with pdksh, see man ksh for details, especially the entry on the builtin command time ... cheers, drl

Thanks drl, I think you hit the nail on the head. Once I enclosed the time and command in a brace with a semicolon the output was written into the output files as well as the screen. :b:

I am using AIX and it looks like the output of the time command is "real xh xm xs". As for the /usr/bin/time command is "Real x.x" (seconds). So I should be able to use the /usr/bin/time and calculate the minutes using the seconds it returns. :slight_smile:

Thanks for you help. :smiley:

Hi.

Glad it seems to be solved. It was really the man page that helped, as they do for so many problems. One slight drawback is that some man pages are very long, and some are poorly written, so it can be a chore to look through them. I think the Solaris man pages usually are the best, although the commands in Solaris, while very stable, are not as flexible as those of most Linux distributions.

Luckily, the shell hides a lot of the differences of OS platforms. However, it's good that you mentioned the Korn shell, and it probably wouldn't hurt to mention AIX, just in case that makes a difference -- I don't use AIX any longer, but I suspect many others here do ... cheers, drl

Ksh and Bash also have a built-in variable $SECONDS which can be used for timing...

SECONDS=0
:
: script commands
:
echo elapsed time $((SECONDS/60)) minutes $((SECONDS%60)) seconds

Thanks for the example Ygor. :smiley:

I'd been using a simple "...`expr ${LOOPREQD} \* ${LOOPSLEEP} / 60` Minute/s." to do similar, though not entirely accurate. (was indicative to a degree)

Is there a listing anywhere of built-in variables for Ksh ??

Ygor, That might work even better for what I am doing. I take it that you can also use

echo elapsed time $((SECONDS/60/60) hours $((SECONDS/60)) minutes $((SECONDS%60)) seconds

to also show hours.

But how would you show total elapsed minutes?

Try...

SECONDS=12345
echo elapsed time $((SECONDS/60)) minutes $((SECONDS%60)) seconds
echo elapsed time $(((SECONDS/60)/60)) hours $(((SECONDS/60)%60)) minutes $((SECONDS%60)) seconds

Gives...

elapsed time 205 minutes 45 seconds
elapsed time 3 hours 25 minutes 45 seconds