
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!
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Shell Script Programming
(Article 6830)
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!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 PMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|