How to measure duration in seconds in a shell script?

I want to know how long an operation takes in a shell linux script. How can i do this?

+70
linux shell timing
Mar 01 2018-11-11T00:
source share
8 answers

Using a time command, as others have suggested, is a good idea.

Another option is to use the $ SECONDS magic built-in variable, which contains the number of seconds since the script was run. You can say:

START_TIME=$SECONDS dosomething ELAPSED_TIME=$(($SECONDS - $START_TIME)) 

I think this is bash-specific, but since you are working on Linux, I assume you are using bash.

+99
Mar 01 2018-11-11T00:
source share

Use the time command. time ls /bin .

+39
Mar 01 2018-11-11T00:
source share

Try the following example:

 START_TIME=$SECONDS # do something sleep 65 ELAPSED_TIME=$(($SECONDS - $START_TIME)) echo "$(($ELAPSED_TIME/60)) min $(($ELAPSED_TIME%60)) sec" #> 1 min 5 sec 
+34
May 05 '11 at 2:03 pm
source share

You can use the time command. Just add β€œtime” before the command you want to measure by duration. (Source: http://unixhelp.ed.ac.uk/CGI/man-cgi?time )

+5
Mar 01 2018-11-11T00:
source share

Many answers mention $SECONDS , but this variable is even better than they think :

Assignment to this variable resets the score to the assigned value, and the extended value becomes the assigned value plus the number of seconds from the moment of assignment.

This means that you can simply request this variable directly at the end of your script to display the elapsed time:

 #!/usr/bin/env bash # Do stuff... echo "Script finished in $SECONDS seconds." 

You can also calculate smaller sections, like so:

 #!/usr/bin/env bash # Do stuff SECONDS=0 # Do timed stuff... echo "Timed stuff finished in $SECONDS seconds." 
+5
Mar 10 '19 at 1:27
source share

Here is a script to find the elapsed time in milliseconds. Replace sleep line 60 with the code you want to execute.

 a=0 while [ $a -lt 10 ] do START_TIME=`echo $(($(date +%s%N)/1000000))` sleep 3 END_TIME=`echo $(($(date +%s%N)/1000000))` ELAPSED_TIME=$(($END_TIME - $START_TIME)) echo $ELAPSED_TIME if [ $a -eq 10 ] then break fi a=`expr $a + 1` done 
+4
Mar 24 '15 at 10:42
source share

Just to help someone like me get the error message:

  arithmetic expression: expecting primary: "-" 

Check out your shellscript, which should start with:

 #!/bin/bash 

Hurrah!

+3
Jan 19 2018-12-12T00:
source share

time GNU

I also have a lot of fun with GNU time commands: https://www.gnu.org/software/time/ , which offers some important options compared to the built-in time bash.

Usage example:

 env time --format '%e' --output time.log sleep 1 

Exit:

 1.00 

Explanation:

  • env : find /usr/bin/time instead of the built-in Bash

  • --format '%e' : print time in seconds, see man time .

    Often this is what I want when testing: a single number, not minutes + seconds.

And the important pattern that I often use:

 bench-cmd() ( logfile=time.log echo "cmd $@" >> "$logfile" printf 'time ' >> "$logfile" bench_cmd="env time --append --format '%e' --output '$logfile' $@" eval "$bench_cmd" echo >> "$logfile" ) rm -f time.log bench-cmd sleep 1 bench-cmd sleep 2 bench-cmd sleep 3 cat time.log 

GitHub upstream .

Exit:

 cmd sleep 1 time 1.00 cmd sleep 2 time 2.00 cmd sleep 3 time 3.00 

Explanation:

  • --output : output time to a file.

    By default, the output is sent to stderr, so this parameter is important for the synchronization branch from the stderr command.

  • --append : add to file instead of overwrite.

    This allows me to concentrate all test output in a single file.

0
Jun 15 '19 at 9:56 on
source share



All Articles