Operating System - Linux
1745902 Members
4706 Online
108723 Solutions
New Discussion юеВ

Help Needed For "time" command in HP-UX and Linux

 
Sujoy
Occasional Advisor

Help Needed For "time" command in HP-UX and Linux

Hi,

I need to use 'time ' command to calculate the "real" time elapsed during execution of the , from shell and redirect the output in a file or may be in a variable.

man page of 'time' in HP-UX says that the times (real,user and sys) will be printed in stderr.
linux (RHEL) man page of time says that the times (real,user and sys) will be printed in stdout.

But any amount of redirection attempt like below are failing: -

time sleep 1 2>/tmp/xxx
time sleep 1 >/tmp/xx
time sleep 1 |tee /tmp/xxx
time sleep 1|grep real >/tmp/xxx
TIME_ELAPSED=`time sleep 1`; echo $TIME_ELAPSED

etc...in both HP-UX and RHEL.

I also tried to use 'timex' as workaround which does not show this redirection problem. But 'timex' is not available in Linux, hence i need to fall back on 'time' only which works for both HP-UX and Linux.

It shall be extremely helpful if you can tell me how to capture the output of 'time' command from shell into file or a shell variable. Also, if any other command can complement my requirement of calculating the elapsed time of an executed command that would also be very helpful.

Thanking you in advance.

Sujoy Sarkar
Hewlett-Packard Co.
Bangalore - India
Ph: +91 80 2205 3084
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: Help Needed For "time" command in HP-UX and Linux

good utils for this kind of work.

http://hpux.ws/merijn/caljd-2.23.sh

http://hpux.ws/merijn/caljd-2.2.pl

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ivan Ferreira
Honored Contributor

Re: Help Needed For "time" command in HP-UX and Linux

If you change to ksh shell, you can use the time --output option.

#!/bin/ksh
time --output time.out ls


Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Mike Stroyan
Honored Contributor

Re: Help Needed For "time" command in HP-UX and Linux

The trick is that the time command is actually a shell builtin for some shells. You can use quoting as 'time' or use a full path as /usr/bin/time to get the actual command described by "man time". You can also use parentheses to run the time builtin in a subshell to redirect its output.

'time' sleep 1 2> /tmp/xx

/usr/bin/time sleep 1 2> /tmp/xx

(time sleep 1) 2> /tmp/xx
Sujoy
Occasional Advisor

Re: Help Needed For "time" command in HP-UX and Linux

Sorry for response too late.
The solutions really helped me get going.
Thanks.
\Sujoy
Hewlett-Packard Co.