473,473 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to capture output of unix time command?

3 New Member
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 17666
numberwhun
3,509 Recognized Expert Moderator Specialist
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
lalnamar
3 New Member
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 Recognized Expert Moderator Specialist
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 Recognized Expert Top Contributor
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
lalnamar
3 New Member
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 New Member
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 New Member
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 Recognized Expert Moderator Specialist
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 Recognized Expert Top Contributor
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.