Jun 6, 2011

Linux stopwatch / get the program execution time in shell

Simple time/ stopwatch of program execution in the shell (presuming the clock on the server runs synced to NTP so it's accurate, otherwise it gets inaccurate after longer execution period):

create a shell script (ie. ftpscript.sh) and use the following code (modify the line with execute shell command):
start=$(date +%s) ## get current time/date to $start
{ ./lftpscript.sh; } ## execute shell command / program ./lftpscript.sh in this example
stopped=$(date +%s)
thetime=$(($stopped-$start)) ## doing some math in shell calculating time difference

echo "Time needed: " $(date -d "1970-01-01 $thetime sec" +"%H:%M:%S") / $thetime "secs" ## prints out the time ( like: 00:01:29 / 89 secs )
Full post / more »»