By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

How to capture output of unix time command?

P: 3
I want to execute a unix time command using perl, and capture the output (specifically, the time it takes the command to execute).

i tried using the system command:
system("time command ");
however, when i try to capture the output, the capture output does not contain the amount of time it took to execute

when i try to do:
$var = `time command `;
there is the same problem (the ouput from the command is saved in the variable; however, the time is not)

So, how do i capture the time it takes a unix command to execute?
Jul 17 '07 #1
Share this Question
Share on Google+
10 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Your script is doing exactly what you designed it to do. It is storing the output of the command you issued in the variable you specified. Even on the command line the command won't tell you how long it took to execute. It sounds like you want to do some sort of benchmarking of Perl while executing system commands. Unfortunately, I don't have any experience with benchmarking and couldn't be much help.

Regards,

Jeff
Jul 17 '07 #2

P: 3
when i type in "time command " at the command line i get the following:

...
lines of output
....
real 0.5
user 0.4
sys 0.0

I want to store ALL the above output in a file (using a perl script). When i try to do this, i am only able to store the "lines of ouput" part. I am unable to store those last three lines, and i cant figure out why I am unable to.
Jul 17 '07 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Ok, have you tried this"

@var = `time command`;

That should take each line and put it into its own element. So, element [0] would be "lines of output"

Regards,

Jeff
Jul 17 '07 #4

miller
Expert 100+
P: 1,089
I want to execute a unix time command using perl, and capture the output (specifically, the time it takes the command to execute).

i tried using the system command:
system("time command ");
however, when i try to capture the output, the capture output does not contain the amount of time it took to execute

when i try to do:
$var = `time command `;
there is the same problem (the ouput from the command is saved in the variable; however, the time is not)

So, how do i capture the time it takes a unix command to execute?
I have no experience with the time command in unix. Whenever I need benchmarking, I would simply roll my own using Time::HiRes and the gettimeofday function.

However, throwing together a quick script, this is what I obtained.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. # scratch.pl
  3.  
  4. print "Hello world\n";
  5. sleep 2;
  6. print "Goodbye World\n";
  7.  
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. # scratch2.pl
  3.  
  4. my $x = `time -p perl scratch.pl`;
  5. print $x;
  6.  
And the output of running scratch2.pl

Expand|Select|Wrap|Line Numbers
  1. $ perl scratch2.pl 
  2. real 2.00
  3. user 0.00
  4. sys 0.00
  5. Hello world
  6. Goodbye World
  7.  
Seems to work ok to me.

- Miller
Jul 17 '07 #5

P: 3
Comment out line 5 of scratch2.pl and run the program. The output is:

real 2.00
user 0.00
sys 0.00

Alternatively, add the line - print $x; - to scratch2.pl and run the program (now we are printing $x twice). The output is:

real 2.00
user 0.00
sys 0.00
Hello world
Goodbye World
Hello world
Goodbye World

In other words, the timing information was never captured in the variable $x !
Jul 17 '07 #6

P: 2
owo
I believe the "time" command in unix prints to standard error rather than standard out, which may be why you aren't capturing it. See discussion of capturing standard error at: http://stein.cshl.org/genome_informatics/unix2/index.html#stderr
Aug 28 '07 #7

P: 2
owo
Note: you can also do some shell scripting, and try something like:

( time <command> ) | perl -options '....'

Depending on your shell, I suppose. But this syntax works on at least come tcsh (and csh?) varieties.
Aug 28 '07 #8

numberwhun
Expert Mod 2.5K+
P: 3,503
I believe the "time" command in unix prints to standard error rather than standard out, which may be why you aren't capturing it. See discussion of capturing standard error at: http://stein.cshl.org/genome_informatics/unix2/index.html#stderr
If that is the case, then all he would have to do is add a 2>&1 to the end of his command and it will send the error to wherever he specified the standard out to go to.

Regards,

Jeff
Aug 29 '07 #9

miller
Expert 100+
P: 1,089
perlfaq8 How can I capture STDERR from an external command?

Just change my scratch2.pl script to the following:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. # scratch2.pl
  3.  
  4. my $x = `time -p perl scratch.pl 1>/dev/null 2>&1`;
  5. print $x;
  6.  
- Miller
Aug 30 '07 #10

P: n/a
On Solaris 10 (using bash shell), I had trouble getting `time -p <command> 2>&1` to work. I solved it by executing the time command in its own shell as follows:

TIMING=`bash -c "time ls -l" 2>&1`
Nov 2 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.