You were never here, you didn't see anything..

Shell Script Timing


It took me a little time to find a way to get the amount of time it took for a shell script to execute. Every time I looked for it on the web I would get all kinds of other wild stuff. Well I had come across a way to time PHP execution times and after reading that figured how different could it be?

Using basic principals the following was derived. It takes a start time and an end time, gets the difference and then formats that difference using the date command. Thats right, date will format a string for you based on certain input data.

To start the timing use:

BEGIN="$(date +%s)"

To end the timing use the following. Of course you will want this as close to the end as you can get it if you use it inside the script. So far I have only used it in my backup-usb script and it ends just before the info is spit out on screen at the very end.

FINISH="$(date +%s)"

Then calculate the difference and then format it as needed:

TTIME="$(expr $FINISH - $BEGIN)"
YEAR="$(date +%Y-%m-%d)"
RTIME=`date -d "$YEAR $TTIME sec" +"%H:%M:%S"

The variable RTIME is assigned the run time, hence the name. Do with it what you please!

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This blog is kept spam free by WP-SpamFree.

Most who fail…

Most who fail have yet to really try!