473,750 Members | 2,638 Online
Bytes | Software Development & Data Engineering Community
+ 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 17702
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_informat ics/unix2/index.html#stde rr
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_informat ics/unix2/index.html#stde rr
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

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

Similar topics

8
3702
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 of these, but I can't find any way to do them both at the same time. So far, I've got the following options:
4
4061
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 is #Makefile and some scripts to give output #numbers #Change till sign #END
3
8026
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 command such as system("cat /test.txt | grep something ") or whatever DOS or linux command and assign the output of that command to the variable str;
5
35681
by: francescomoi | last post by:
I'm trying to show current Unix time in C. Is it possible?
4
15076
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 this program returns. I'm just missing the stepshere. I know that I can set the Shell command to an integer,but this only returns a 0 to me telling me that it executed, notwhat is being returned to the console by that application. Isthere a way to...
2
3001
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) when an event occured. When I try to access that database with psycopg2, I get rounded values
4
8927
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 didn't seem to work well. I haven't had a lot of need to do this recently, but we're trying to automate some processes right now that require us to trap the output of a command line and react accordingly. Any suggestions on the best method for...
37
4687
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 leap second? By "Unix time" I mean the integer returned by time() and similar functions. I'm concerned about the "down" case. Some of the software I've written
6
16943
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 C:\\Dave\\C-programming\\Password Cracker\\ & ftp -s:ftpscript.txt ftp.collateraldesign.co.uk"); printf ("The value returned was: %d.\n",i); The value returned is 0. However, I want to know if there was an error logging on, how can I know whether the...
0
8838
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
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 we have to send another system
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.