0

I want to measure in shell how long it takes to execute a script. I use the built in command 'time'.

For example for

time (sleep 5)

I get

real    0m5,003s
user    0m0,001s
sys     0m0,000s

Now I want to compare if the real duration (0m5,003s) is less than 10 seconds.

How can I access the real time to use it?

tripleee
  • 175,061
  • 34
  • 275
  • 318
psad
  • 23
  • 5
  • I cannot type a greeting haha. So Hello everyone :) – psad May 21 '21 at 14:46
  • 1
    Does this answer your question? [Parsing the output of Bash's time builtin](https://stackoverflow.com/questions/26784870/parsing-the-output-of-bashs-time-builtin) – U880D May 21 '21 at 14:53
  • Hi @U880D thank you for the link. I am a beginner in shell and I dont really understand how to do it. I want to save the real time from the output and use it for further applications. – psad May 21 '21 at 15:12
  • No problem, we started all as beginners. The solution to your problem is given [there](https://stackoverflow.com/a/26785328/6771046). I would almost repeat only what is already explained in a very good way. You may start with having a look at the structure `echo $(TIMEFORMAT='%R'; { time sleep 5; } 2>&1)`. – U880D May 21 '21 at 15:27
  • @psad: This might help : https://serverfault.com/questions/175376/redirect-output-of-time-command-in-unix-into-a-variable-in-bash – User123 May 21 '21 at 15:50

2 Answers2

3

With bash, if you just need the duration in seconds, use the builtin SECONDS variable:

SECONDS=0
some long running process here
duration=$SECONDS

if ((duration > 10)); then
    echo "it took a long time"
fi
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
-1

Please see below code which will redirect into a variable and different options by which you can achieve it:
Option 1:

# !/usr/bin/bash
var=`(time (sleep 5)) 2>&1 | grep real`

Output: real 0m5.069s

You can execute your script inside it as below :

# !/usr/bin/bash
var=`(time sh  ./yourscript.sh) 2>&1 | grep real`

Option 2: Get time taken by the script to execute

# !/usr/bin/bash
start=$(date +%s)
# Execute another script
sh  ./yourscript.sh
end=$(date +%s)
diff=$(( $end- $start))
echo "It took $diff seconds"

Output : It took 5 seconds

Option 3:

# !/usr/bin/bash
#var=$( TIMEFORMAT="%R"; { time  (sleep 5); } 2>&1 )  
var=$( TIMEFORMAT="%R"; { time  sh  ./yourscript.sh; } 2>&1 ) 

Based on good feedback of @Charles Duffy: Changed some parts and
If targeting bash 4.3 or later, consider printf -v start '%(%s)T' -1 as a much more efficient alternative to start=$(date +%s).

Altaf
  • 2,838
  • 1
  • 16
  • 8
  • Why use backticks (rather than modern command substitution syntax) anywhere at all? – Charles Duffy May 21 '21 at 17:46
  • BTW, re: `START`/`END`/`DIFF`, consider using lower-case variable names as recommended by the POSIX spec @ https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html (read keeping in mind that since setting a shell variable overwrites any like-named environment variable, namespace conventions necessarily apply to both). – Charles Duffy May 21 '21 at 17:47
  • Also, if targeting bash 4.3 or later, consider `printf -v start '%(%s)T' -1` as a much more efficient alternative to `start=$(date +%s)`. – Charles Duffy May 21 '21 at 17:48
  • Also, `echo ${var}` has the bugs described in [I just assigned a variable, but `echo $variable` prints something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). `echo "$var"` has the same number of characters for more-correct output; whereas `printf '%s\n' "$var"` is longer, but [also correct even in the corner cases where even `echo "$var"` gets it wrong](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo). – Charles Duffy May 21 '21 at 17:50