Operating System - Linux
1751832 Members
5041 Online
108782 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
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
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

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

Hi:

Interesting, an old 10.20 'time' works. On 11.11 I obtain your result. I think "broken" is the word.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

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

Hi (again):

No, I think I'm crazy. This works just fine on 11.11 :

# time sleep 1 2>/tmp/xxx

I was typing "times" instead. :-((

Regards!

...JRF...
Sujoy
Occasional Advisor

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

But "time sleep 1 2>/tmp/xxx"

does not get the output into /tmp/xxx

in "HP-UX ccux1 B.11.11 U 9000/800 1861980610 unlimited-user license"

also in "HP-UX ccux3 B.11.23 U ia64 0943546897 unlimited-user license"

It only throws the output in screen.

--Sujoy
Patrick Wallek
Honored Contributor

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

For me it is working correctly on 11.0, 11.11 and 11.23.

# time sleep 1 2>/tmp/xxx

# /tmp/xxx
real 1.0
user 0.0
sys 0.0
Stephen Keane
Honored Contributor

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

Doesn't work for me on 11.11 either. Maybe it's a patch. The system I'm using is (as far as I know) fully patched up to Sept 2005 patch bundle.

Stephen Keane
Honored Contributor

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

Got it, it works in sh, but not in ksh

Mike Stroyan
Honored Contributor
Solution

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

The trick is that time is a shell built-in command for some shells. You can use a quoted command or full path to get the actual command that matches 'man time'. Here is a sample from bash on linux.

$ time sleep 1

real 0m1.004s
user 0m0.001s
sys 0m0.001s
$ 'time' sleep 1
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+200minor)pagefaults 0swaps
$ /usr/bin/time sleep 1
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+199minor)pagefaults 0swaps
Sujoy
Occasional Advisor

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

Thanks. The solutions helped me.