Have you been wondering how you can find an execution time of a process in Linux or Unix system?. This guide will show you a number of tools which comes in handy when trying to find execution time of a process in Linux.
At times you may have to work on slow executing processes or having a slow internet or running a program that you need to track its execution time. Let's look at the top tools that you should try out for this. Every command shown in this guide has been tested on Ubuntu 16.04 server and on CentOS 7.
Gnomon is a utility used to annotate console logging statements with timestamps and find slow processes on a Linux system. This tool is useful for long-running processes where you'd like a historical record of what's taking so long.
Since Gnomon is a tool written in Node.js, you need Node.js installed on your system so that you can install gnomon with npm package manager. Once you have npm tool present on your Linux system, then proceed to install them using:
$ npm install -g gnomon /usr/local/bin/gnomon -> /usr/local/lib/node_modules/gnomon/bin/gnomon + firstname.lastname@example.org added 56 packages in 13.076s
To prepend a timestamp to each line, you need to pipe the command to gnomon. It will indicate how long the process took to execute. By default, gnomon will display the seconds elapsed between each line, but that is configurable.
Take a look at below example which prints the time taken to do 5 times ping request to google DNS server.
$ ping -c 5 18.104.22.168 | gnomon 0.0049s PING 22.214.171.124 (126.96.36.199): 56 data bytes 0.3603s 64 bytes from 188.8.131.52: icmp_seq=0 ttl=59 time=179.114 ms 1.0025s 64 bytes from 184.108.40.206: icmp_seq=1 ttl=59 time=182.345 ms 1.0008s 64 bytes from 220.127.116.11: icmp_seq=2 ttl=59 time=183.636 ms 1.0119s 64 bytes from 18.104.22.168: icmp_seq=3 ttl=59 time=181.139 ms 0.0002s 64 bytes from 22.214.171.124: icmp_seq=4 ttl=59 time=190.970 ms 0.0002s 0.0001s --- 126.96.36.199 ping statistics --- 0.0001s 5 packets transmitted, 5 packets received, 0.0% packet loss 0.0020s round-trip min/avg/max/stddev = 179.114/183.441/190.970/4.048 ms 0.0002s Total 3.3842s
The total time elapsed is 3.3842s.
Available Options are:
Below is a list of options available:
-t | --type=<elapsed-line|elapsed-total|absolute> :
Type of timestamp to display.
elapsed-line: Number of seconds that displayed line was the last line.
elapsed-total: Number of seconds since the start of the process.
absolute: An absolute timestamp in UTC.
$ ping -c 3 188.8.131.52 | gnomon --type=elapsed-total 0.0049s PING 184.108.40.206 (220.127.116.11): 56 data bytes 0.2336s 64 bytes from 18.104.22.168: icmp_seq=0 ttl=59 time=46.288 ms 1.2798s 64 bytes from 22.214.171.124: icmp_seq=1 ttl=59 time=35.811 ms 1.2801s 64 bytes from 126.96.36.199: icmp_seq=2 ttl=59 time=80.783 ms 1.2802s 1.2804s --- 188.8.131.52 ping statistics --- 1.2805s 3 packets transmitted, 3 packets received, 0.0% packet loss 1.2821s round-trip min/avg/max/stddev = 35.811/54.294/80.783/19.213 ms 1.2823s Total 1.2824s
-f | --format="format": Format the absolute timestamp, using PHP date format strings. If the type is elapsed-line or elapsed-total, this option is ignored. The default format is "H:i:s.u O"
-r | --real-time=<number|false>
Example to disable real-time update:
# ping -c 3 184.108.40.206 | gnomon --real-time=false 0.0040s PING 220.127.116.11 (18.104.22.168): 56 data bytes 0.7847s 64 bytes from 22.214.171.124: icmp_seq=0 ttl=59 time=69.803 ms 0.9316s 64 bytes from 126.96.36.199: icmp_seq=1 ttl=59 time=140.597 ms 0.0001s 64 bytes from 188.8.131.52: icmp_seq=2 ttl=59 time=68.122 ms 0.0001s 0.0001s --- 184.108.40.206 ping statistics --- 0.0001s 3 packets transmitted, 3 packets received, 0.0% packet loss 0.0020s round-trip min/avg/max/stddev = 68.122/92.841/140.597/33.776 ms Total 1.7229s
-h | --high=seconds : High threshold
-m | --medium=seconds : Medium threshold. Works just like the high threshold described above, but colors the timestamp bright instead.
Check running process time using ps
You can use ps command to check the time a particular process has been running. You need to first find process ID then use it to find elapsed time.
To identify process ID, you can use a tool like pidof
$ pidof mpd 1388
Then use ps with options -o etime to find elapsed running time.
$ ps -p 1388 -o etime ELAPSED 05-11:03:02
etime option displays elapsed time since the process was started, in the form [[DD-]hh:]mm: ss. So from above example, the process has been running for 5 days, 11 hours and 3 minutes. Use etimes option to get elapsed time in seconds.
This command option can also be used for multiple processes. The example below will display start time and the execution time of all processes on my Ubuntu server.
$ ps -eo pid,lstart,etime,args
The output has 4 columns:
PID --> ID of the running process
STARTED --> The time the process was initially started
ELAPSED --> Total running time of the process
COMMAND --> Process executed command
Using time command on Ubuntu
The time command reports how long the command took to execute on a Linux system. You can install it if missing on Ubuntu system using:
$ sudo apt-get install time
time command Usage:
# time [-p] command [arguments...]
The output of time will have:
- The elapsed real time between command invocation and termination.
- The user CPU time.
- The system CPU time.
Consider below example to check disk usage of /root directory.
# time du -sh /root/ 464K /root/ real 0m0.007s user 0m0.002s
From the output, the actual time the command took to execute is 0m0.007s.
Let's do one more, a ping to 220.127.116.11
# time ping -c 3 18.104.22.168 PING 22.214.171.124 (126.96.36.199) 56(84) bytes of data. 64 bytes from 188.8.131.52: icmp_seq=1 ttl=60 time=7.28 ms 64 bytes from 184.108.40.206: icmp_seq=2 ttl=60 time=11.9 ms 64 bytes from 220.127.116.11: icmp_seq=3 ttl=60 time=7.54 ms --- 18.104.22.168 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 7.281/8.925/11.952/2.145 ms real 0m2.059s user 0m0.001s sys 0m
The actual execution time is 2.059 seconds.
Now you know how to get the Linux Process execution time on Linux. The first method is ideal for interactive processes. For processes that run in the background, you can always get their execution time using ps command.