03 Apr, 2010, Zeno wrote in the 1st comment:
Votes: 0
I want to parse the output of the "time" command in *nix. Say if it takes longer than 1sec, print the output to a file.

Would a bash script be simple to do this? All I have right now is printing the entire output to a file, and I just want to print if it's a high time delay.
04 Apr, 2010, Davion wrote in the 2nd comment:
Votes: 0
Zeno said:
I want to parse the output of the "time" command in *nix. Say if it takes longer than 1sec, print the output to a file.

Would a bash script be simple to do this? All I have right now is printing the entire output to a file, and I just want to print if it's a high time delay.


You could use a script in your fav language to simply pipe the command, read the input and parse it with some simple regexp. Once the values are set you can just compare and do what you want with them.
04 Apr, 2010, Zeno wrote in the 3rd comment:
Votes: 0
I considered that, but is that going to throw the timing of "time" off a bit?
04 Apr, 2010, Kline wrote in the 4th comment:
Votes: 0
Is this something you're trying to execute from within the game; to have the MUD fork a proc to the shell?
04 Apr, 2010, Davion wrote in the 5th comment:
Votes: 0
Zeno said:
I considered that, but is that going to throw the timing of "time" off a bit?


I don't see why it should. Maybe if you were timing the execution of 'popen' to execute the command, but you're not.
04 Apr, 2010, Zeno wrote in the 6th comment:
Votes: 0
Nothing to do with the MUD.
04 Apr, 2010, flumpy wrote in the 7th comment:
Votes: 0
I'm pretty sure there's some kind of tautology going on here: you want to check the time, but need to check how long checcking the time takes?

I might be wrong here, but in order to check the amount of time passed after exec'ing time, you would need to use 'time' to do this (to get ms). If you are expecting delays in your time call, you may have finished your first call before the second has finished..

Why are you having issues with calling 'time'?

Edit: are you monitoring another server's time or something?
04 Apr, 2010, kiasyn wrote in the 8th comment:
Votes: 0
Something like this works, but could have memory issues (depending on log size):

#!/usr/bin/ruby
start_time = Time.now
file_to_execute = 'ruby1.9 ./long_app.rb'
seconds_required = 5 # must run for at least 5 seconds to be logged
buffer = `#{file_to_execute} 2>&1`

File.open( 'my.log', 'a' ).write buffer unless Time.now.to_i - start_time.to_i < seconds_required


Also it isn't concurrent … it waits until the program exits before logging.
04 Apr, 2010, flumpy wrote in the 9th comment:
Votes: 0
But my point is, time.now will do the same underlying call (effectively) as the unix time command..

So what does this give you?

Edit: I mean, that ruby script does do what you describe, but what's the point of it? You might as well just do it all in a script, but then you'd be doing it twice, so :shrug:
06 Apr, 2010, ProjectMoon wrote in the 10th comment:
Votes: 0
What is the actual requirement? In most situations it would be better to get the current time in the programming language itself. That makes it more portable and more maintainable. So, why is it best that you parse the output of the date command instead of use the programming language to do it?
06 Apr, 2010, quixadhal wrote in the 11th comment:
Votes: 0
Zeno said:
I want to parse the output of the "time" command in *nix. Say if it takes longer than 1sec, print the output to a file.

Would a bash script be simple to do this? All I have right now is printing the entire output to a file, and I just want to print if it's a high time delay.


First question… what OS are you using, and what shell IN that OS? It makes a difference. :)

Quote
OpenBSD:
# /usr/bin/time tar -ztf /root/OpenBSD/ports.tar.gz >/dev/null
14.63 real 11.20 user 3.14 sys
# /usr/bin/time -p tar -ztf /root/OpenBSD/ports.tar.gz >/dev/null
real 14.76
user 11.21
sys 3.21
# /usr/bin/time -l tar -ztf /root/OpenBSD/ports.tar.gz >/dev/null
14.75 real 11.32 user 3.08 sys
0 maximum resident set size
0 average shared memory size
0 average unshared data size
0 average unshared stack size
256 minor page faults
0 major page faults
0 swaps
267 block input operations
4 block output operations
0 messages sent
0 messages received
0 signals received
9152 voluntary context switches
9286 involuntary context switches


Note that there is also a "time" command built into ksh, one in bash, and I believe in tcsh too.

Quote
# time tar -ztf /root/OpenBSD/ports.tar.gz >/dev/null
0m14.99s real 0m11.25s user 0m3.39s system
0.0/11