71

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.

0

1 Answer 1

119

use time:

$ time somecommand --with=somearguments

time will execute the rest of the command line (in this example somecommand --with=somearguments ) and when the command is done it will print the elapsed time.

example output:

$ time somecommand --with=somearguments
...
... (output of somecommand)
...
real    0m5,020s
user    0m0,010s
sys     0m0,010s

the information that you typically want is real. in this example the command took about 5 seconds. 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


the example output above is from the bash builtin. time is a builtin command in most shells. there is also the system time. it has a different output format but otherwise behaves mostly the same as the shell builtin. more about that later.

if you just want to see the time it takes for a task then just put time in front of your command and be done with it. it does not matter if you use the shell builtin or system command. but if you want to measure time in a script you will want to use the system command.

to use the system command you do it like this:

$ /usr/bin/time somecommand --what=islove

or

$ command time somecommand --baby=donthurtme

there are more but that is outside the scope of this question and answer.

invoking the binary directly (/usr/bin/time) is probably the most safe and portable method.

the command method works in bash and maybe some other shells. but might have different behaviour if used in scripts or functions or subshells or whatnot.

for more information about the difference of a shell builtin and a system command see here: What is the difference between a builtin command and one that is not?


example output using system time:

$ /usr/bin/time somecommand --what=isyourname
...
... (output of somecommand)
...
0.00user 0.01system 0:06.02elapsed 0%CPU (0avgtext+0avgdata 3656maxresident)k
0inputs+0outputs (0major+1089minor)pagefaults 0swaps

the information that you typically want is elapsed. in this example the command took about 6 seconds. for the meaning of the other numbers i have not found a good explanation in the internet. you have to read the man page: http://man7.org/linux/man-pages/man1/time.1.html

you can change the output of the system time. for example use -p to get output similar to the shell builtin time:

$ /usr/bin/time -p somecommand --what=isyourquest
...
real 0:06.02
user 0.00
sys 0.01

or use -f to write your own format:

$ /usr/bin/time -f %E somecommand --what=istheairspeedvelocityofanunladenswallow
...
0:06.02

%E is the "elapsed" part. the part you are usually most interested in.

for completeness: you can also change the bash builtin time output format by changing this variable: TIMEFORMAT. more info: https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-TIMEFORMAT


how to redirect or capture the output

for demonstration observe command hellostdoutstderr:

#!/bin/sh
sleep 0.5
echo "hello out"
echo "hello err" >&2

example invocations:

 $ ./hellostdoutstderr 
hello out
hello err

capture stdout and stderr separately

$ ./hellostdoutstderr >stdoutfile 2>stderrfile

$ cat stdoutfile
hello out

$ cat stderrfile
hello err

the system time prints to stderr so it is captured with the stderr redirect

$ /usr/bin/time ./hellostdoutstderr >stdoutfile 2>stderrfile

$ cat stdoutfile
hello out

$ cat stderrfile
hello err
0.00user 0.00system 0:00.50elapsed 1%CPU (0avgtext+0avgdata 3672maxresident)k
0inputs+16outputs (0major+311minor)pagefaults 0swaps

you can tell the system time to print to a separate file

$ /usr/bin/time -o timeoutfile ./hellostdoutstderr >stdoutfile 2>stderrfile

$ cat stdoutfile
hello out

$ cat stderrfile
hello err

$ cat timeoutfile
0.00user 0.00system 0:00.50elapsed 1%CPU (0avgtext+0avgdata 3676maxresident)k
0inputs+16outputs (0major+309minor)pagefaults 0swaps

note that the bash builtin time always prints to the terminal even if stdout and stderr is redirected. this is possible because it is a builtin and can do whatever it likes (in the shell)

$ time ./hellostdoutstderr >stdoutfile 2>stderrfile

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

in this example stdout and stderr are both redirected to file. but bash builtin time still prints to terminal.

with tricks it is still possible to capture the output of the bash builtin. but why fight bash if it is easier to use system command. if you are so inclined here is more info: https://www.cyberciti.biz/faq/unix-linux-time-command-examples-usage-syntax/


to time more complex commands you have severals options

if you just want to time two command one after the other

$ time { command1 ; command2 ; }

also works with pipe

$ time { command1 | command2 ; }

for more complex stuff

$ time sh -c ' complex command chain '

but mind the quoting and other shenanigans. better put the commands in a script and time the script:

$ time ./script.sh

TL;DR: add time before your command. if you want to measure time inside a script use /usr/bin/time.

6
  • 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, 2013 at 22:24
  • 2
    Additionally you can do time { command; command2; }
    – sparticvs
    Aug 14, 2013 at 0:33
  • 2
    I dont get it, what are the comments for? what is the output of this function?
    – j0h
    Apr 5, 2016 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, 2018 at 19:32
  • 1
    i elaborated the answer. now give me upvotes. sweet sweet upvotes.
    – Lesmana
    Apr 24, 2018 at 17:10

Not the answer you're looking for? Browse other questions tagged .