|
How can I time portions of a Linux shell script?Just picked up your book Wicked Cool Shell Scripts and already have one quick question: While working on the startup scripts, I would like to place log timers at certain portions of scripts. Is it possible to log point A at he beginning of a section, do the processing, log point B at the end of the section and then log B minus A so I know exactly how long a section took to process? On first glance, I thought that this would be a particularly tough challenge, with some sort of requirement to parse and translate date and time stamps, but then I realized that it's actually remarkably straightforward as long as you have a decent version of the date command on your system. What you want to use is the "+%s" string format to the date command. If you have a good version of the command, it'll output the number of seconds since the Epoch as a numeric value: $ date +%s
1157490628 Your resolution would be in seconds, but it should be quite easy to run this command just before and just after the block in question, the do a simple subtraction to figure out the elapsed time. Like this: before="$(date +%s)"
long block of slow code after="$(date +%s)" elapsed_seconds="$(expr $after - $before)" echo Elapsed time for code block: $elapsed_seconds If you want to go from Epoch time to a more coherent date/time string, you can use the "-r" flag to date. You can figure out the "after" time, for example, this way: echo Finished up at $(date -r $after)
But it turns out that you can also specify a format string to the date command even with the "-r" flag in use, so you could also get the HH:MM:SS value only of the difference between the two values like this: echo Elapsed time: $(date -r $elapsed_seconds +%H:%M:%S)
Hope that helps you see a solution path for your script!
Categorized:
Shell Script Programming
(Article 6830,
Written by Dave Taylor)
Tagged: date math, linux, process timing, shell script programming Previous: How do I add album artwork to my Apple iTunes library? Next: How can I find out what keywords cost on AdWords? Subscribe!
Hi there. Alternatively, most shells have a 'time' facility built-in. This works in bash; your milage may vary for other shells. ---------------------------------- { time { { time { This will output real, user and cpu timing information into the text files specified. (The slightly odd-looking code blocks are needed to appropriately redirect standard error to a file. See http://www.cs.tut.fi/~jarvi/tips/bash.html for an explanation.) Hope this helps. Regards, thanks a lot!! i just grabbed your code, plugged it into my script and now have a timer-based control over my child processes. very useful! Posted by: vicki at September 10, 2007 5:55 PMline: should actually read: -d displays the time given by input string. Kept getting "file does not exist error", found out that the "-d" flag was causing error. Thanks for the script... saved me a bit of sanity and time... and these days, we need all the sanity we can get. b@ Posted by: b@ at July 14, 2008 10:57 AMHi iwant to set my server time automaticlly with conecting with ssh to another host ant get correct time and change my server time in crontb enviroment please help me thanks Posted by: farid at July 21, 2008 12:27 AMHad to adjust the last line to get this to work for me -- final script below: #!/bin/bash Perfect! I had a number of scripts that I wanted to track session time on, along with other time variations. I have been able to use all of the information provided here with success and "right-out-of-the-box". Finally, I can call some of my scripts completed. Thanks much! Posted by: aitd at October 31, 2008 10:01 PM
Luckily the shell date arithmetic is pretty simple. You don't even need 'bc'. (( DIFF = NOW - START )) The printf adds leading zeros so the columns don't float. Not as slick as 'date -d', but the code above works for about a year (which should cover anything in real life). Posted by: Rich at June 12, 2009 12:04 PMOn some systems the folowing command, will give unwanted numbers in the hour column (below) $ echo $(date -r 110 +%H:%M:%S) This is because you're reporting elapsed time since the epoch, but if you're not on GMT, then the epoch won't start at 00:00:00. (On EDT you get 19:00:00 for 0). Therefore you need to report in UTC as follows: $ echo $(date -u -r 110 +%H:%M:%S)
Very Nice! Thanks Dave - exactly what I needed! Posted by: PeteW at July 29, 2011 8:48 AMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
Recommended
Recent Entries
Search
I Need Help!
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and RSS Feeds Building Web Site Traffic Business and Management CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above Facebook Help Google Plus Help HTML and CSS Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Mac OS X Help Pay Per Click (PPC) Advertising Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help WordPress Help |