473,375 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 software developers and data experts.

How to capture output of unix time command?

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
10 17656
numberwhun
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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
1,089 Expert 1GB
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
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
owo
2
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
owo
2
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
3,509 Expert Mod 2GB
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
1,089 Expert 1GB
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
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

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

Similar topics

8
by: Hugh Macdonald | last post by:
I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it) I can do one or other...
4
by: rkoida | last post by:
Hello evryone I am a newbie to python. I have a makefile which i can compile in UNIX/LINUX, But i I am planning to write a python script which actually does what my MAKEFILE does. The make file...
3
by: Glen | last post by:
Hi, I just have a quick question Im trying to initialize a string (or char) variable with the information created by the output of a shell (or DOS) command.. Example: I want to execute a...
5
by: francescomoi | last post by:
I'm trying to show current Unix time in C. Is it possible?
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
2
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I've inserted a couple hundred rows in a table in Postgres via psycopg2. The first field of each row is a certain unix time (since epoch)...
4
by: Peter A. Schott | last post by:
Not sure what I should do here. I know that DOS/CMD can capture the output of EXE files in Win32. I know that os.popen() has been recommended for this, but the couple of times I've tried, it...
37
by: David T. Ashley | last post by:
I have Red Hat Enterprise Linux 4. I was just reading up about UTC and leap seconds. Is it true on my system that the Unix time may skip up or down by one second at midnight when there is a...
6
by: niskin | last post by:
I am running a system command and I need to capture the output so that, if the command has done what it is meant to, the program does something different. If I do this: int i; i=system("cd...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.