Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

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?


Dave's Answer:

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.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Hi there.

Alternatively, most shells have a 'time' facility built-in. This works in bash; your milage may vary for other shells.

----------------------------------
#!/usr/bin/bash

{ time {
  # Your section 1 code goes here
} } 2> timing_section_1.txt

{ time {
  # Your section 2 code goes here
} } 2> timing_section_2.txt
----------------------------------

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,
NeilS.

Posted by: NeilS at September 13, 2006 8:27 AM

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 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









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!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]