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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
example output
$ time longrunningcommand --takeyourtime ... output of longrunningcommand ... real 0m5,020s user 0m0,010s sys 0m0,010s
the interesting information is real 0m5,020s. that means 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
time is a builtin command in most shells. the example output above is from the bash builtin. sometimes you will want to use the “system time”. for example when you need a consistent output format that is not dependent on the shell.
for more information on the difference of a shell builtin and a system command read here: What is the difference between a builtin command and one that is not?
if you want to use the system time do it like this:
$ /usr/bin/time longrunningcommand --getsomecoffee $ time longrunningcommand --callmom $ command time longrunningcommand --mowthelawn
the backslash (time) works in bash and maybe some other shells. command time works in most shells. /usr/bin/time should work in all shells.
example output using system time:
$ /usr/bin/time ./longrunningcommand --callmom ... output of longrunningcommand 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. that means the command took about 6 seconds. 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. use -p to get output similar to the shell builtin time.
$ /usr/bin/time -p sleep 0.5 real 0.50 user 0.00 sys 0.00
use -f to write your own format. %E is the “elapsed” part. that is the part you are usually most interested in.
$ /usr/bin/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
capture stdout and stderr separately
$ ./hellostdoutstderr >stdout 2>stderr $ cat stdout stdout $ cat stderr stderr
the system time prints to stderr so it is captured in the stderr redirect
$ /usr/bin/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 the system time to print to a separate file
$ /usr/bin/time -o timeout ./hellostdoutstderr >stdout 2>stderr $ cat stdout stdout $ cat stderr stderr $ cat timeout 0.00user 0.00system 0:00.50elapsed 1%CPU (0avgtext+0avgdata 3676maxresident)k 0inputs+16outputs (0major+309minor)pagefaults 0swaps
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 >stdout 2>stderr real 0m0,511s user 0m0,005s sys 0m0,006s
(stdout and stderr are captured but time can still print to shell)
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/
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0