30

How would you find out how long a running process took to complete?

Example:

 date; dd bs=1m if=/foo of=bar; date

^This example only has 1 second of resolution.

Any shell is acceptable.

56

use time:

$ time longrunningcommand --takeyourtime

time will execute the rest of the command line as a command (in this example longrunningcommand --takeyourtime) and when the command is done it will print the elapsed time.

another example: time foo --bar will execute foo --bar and wait for it to finish and then print the elapsed time.

time is a builtin command in most shells. the difference of the shell builtin and the system command is mostly the format of the output. see below for more elaboration on the differences.

if you want to use the system time do it like this:

$ /usr/bin/time longrunningcommand --getsomecoffee

or like this:

$ \time longrunningcommand --callmom
$ command time longrunningcommand --callmom

the backslash works in bash and maybe some other shells. command works in most shells.


more elaborate examples

example longrunningcommand:

#!/bin/sh

echosleep() {
  seq $1 | while read tick; do
    echo $tick
    sleep 1
  done
  echo done
}

case $1 in
  --takeyourtime) echosleep 4 ;;
  --getsomecoffee) echosleep 5 ;;
  --callmom) echosleep 6 ;;
  *) echo wat ;;
esac

example invocation:

$ ./longrunningcommand --takeyourtime
1
2
3
4
done

(with delay between each line of output)

example invocation using bash builtin time:

$ time ./longrunningcommand --getsomecoffee
1
2
3
4
5
done

real    0m5,020s
user    0m0,010s
sys 0m0,010s

the interesting information is real 0m5,020s. for more information about the other numbers see here: https://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1

example invocation using system time:

$ \time ./longrunningcommand --callmom
1
2
3
4
5
6
done
0.00user 0.01system 0:06.02elapsed 0%CPU (0avgtext+0avgdata 3656maxresident)k
0inputs+0outputs (0major+1089minor)pagefaults 0swaps

the interesting information is 0:06.02elapsed. for the meaning of the other numbers read the man page of time: http://man7.org/linux/man-pages/man1/time.1.html

you can change the output of the system time. observe:

$ \time sleep 0.5
0.00user 0.00system 0:00.50elapsed 0%CPU (0avgtext+0avgdata 2376maxresident)k
0inputs+0outputs (0major+81minor)pagefaults 0swaps
$ \time -p sleep 0.5
real 0.50
user 0.00
sys 0.00
$ \time -f %E sleep 0.5
0:00.50

how to redirect or capture the output

for demonstration observe command hellostdoutstderr:

#!/bin/sh
sleep 0.5
echo stdout
echo stderr >&2

example invocations:

 $ ./hellostdoutstderr 
stdout
stderr
$ ./hellostdoutstderr >stdout 2>stderr
$ cat stdout
stdout
$ cat stderr
stderr

the bash builtin time prints the terminal even if stdout and stderr is redirected because it is a builtin and can do whatever it likes (in the shell)

$ time ./hellostdoutstderr >stdout 2>stderr

real    0m0,511s
user    0m0,005s
sys 0m0,006s

to still redirect this output read here: https://stackoverflow.com/questions/18348593/how-can-i-output-from-usr-bin-time-to-a-file-at-the-right-location-within-execu

or here: https://www.cyberciti.biz/faq/unix-linux-time-command-examples-usage-syntax/

the system time prints to stderr like it should

$ \time ./hellostdoutstderr >stdout 2>stderr
$ cat stdout
stdout
$ cat stderr
stderr
0.00user 0.00system 0:00.50elapsed 1%CPU (0avgtext+0avgdata 3672maxresident)k
0inputs+16outputs (0major+311minor)pagefaults 0swaps

you can tell time to print to a separate file

$ \time -o timeout ./hellostdoutstderr >stdout 2>stderr
$ cat stderr
stderr
$ cat timeout 
0.00user 0.00system 0:00.50elapsed 1%CPU (0avgtext+0avgdata 3676maxresident)k
0inputs+16outputs (0major+309minor)pagefaults 0swaps
  • 2
    Note that \ is a shortcut version of the command function of bash, so command time longrunningcommand --pedantic-comments is the same :) – Drav Sloan Aug 13 '13 at 22:24
  • 2
    Additionally you can do time { command; command2; } – sparticvs Aug 14 '13 at 0:33
  • 2
    I dont get it, what are the comments for? what is the output of this function? – j0h Apr 5 '16 at 14:58
  • I'm with @j0h -- how do you actually use this? You've shown how to invoke it, but not how to acquire the result. (Experimentally, I've determined that the result is printed to stdout if the command has no output of its own, but what if it does?) And what are the meanings of --getsomecoffee and --callmom? Does this somehow label the thing being timed? This answer needs improvement. (And yes, I'm aware two years have gone by.) – JakeRobb Apr 23 '18 at 19:32
  • 1
    i elaborated the answer. now give me upvotes. sweet sweet upvotes. – lesmana Apr 24 '18 at 17:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.