Linux Measure Process Execution Time, When Already Started

Say you have issued a command in your servers. Typically the command might either backup something or perform a critical hot fix.

Surely you know the start time of the process. But when it will end? How can you find the execution time, when the process has already been started?

Linux measure the execution time of a running process


Original Article: https://dennyzhang.com/process_execution

Connect with Denny In LinkedIn Or MailList


Getting the start time of a running process is easy. Either use ps cli or query /proc filesystem directly.

  • Query start time via ps command line. [1]
# Show process start time by pid
ps -o lstart= -p $pid

# Show process start time, cmd by pid.
ps -o lstart,cmd $pid

  • Query start time via /proc file system directly. [2]
awk '{print "CPU time: " $14+$15; print "start time: " $22}' \
  /proc/$pid/stat

Getting the process execution time is also easy, if we can control how it is started. Simply use time $command. [3]

Wrap the command by adding time as a prefix. Super easy. Isn’t it?

time cp /etc/hosts /tmp/
# real    0m0.004s
# user    0m0.000s
# sys     0m0.000s

But what if you have forgotten to add the time prefix somehow? However unfortunately you have no estimation of when it will finish. How can you still get the execution time, while the process is already running?

Well, you can certainly keep watching the screen, and figure it out sooner or later. A pro wouldn’t do that. Pretty boring and a waste of time!

Check below commands.

  • As most linux users, we all know the watch command, right?
  • And tee command might be less well-known. It reads from standard input, and writes to both standard output and files.
# Record process start time to a log file
ps -o lstart= -p $pid > /tmp/watch_process_$pid.log

# Check process every 2 seconds, using watch command.
# When process is running, record current time to log file
watch "ps -o lstart= -p $pid && \
  (date | tee -a /tmp/watch_process_$pid.log)"

From the logfile, we can get:

  • The process starting time from the first entry of the log file.
  • The ending time from the last entry of the log file.

Why? if the process has finished, the ps command will fail, and the following clauses will be skipped. So with this method, we can monitor any existing process. Sounds great, doesn’t it?

Any further improvement?

Get a timely notification, when the process ends. The process could has been finished for several hours, before you even remember to login and check the status via logfile.

Let’s say you have wrapped up send_alert.sh. This script will send alerts properly. Make a slight change, and you will get alerts properly.

Then we’re all set!

# Record process start time to a log file
ps -o lstart= -p $pid > /tmp/watch_process_$pid.log

# Check process every 2 seconds, using watch command.
# When process is running, record current time to log file.
# When process has finished, send_alert.sh will be executed.
watch "(ps -o lstart= -p $pid || (send_alert.sh && false)) \
  && date | tee -a /tmp/watch_process_$pid.log"

Footnotes:

[1] www.cyberciti.biz/faq/how-to-check-how-long-a-process-has-been-running/
[2] unix.stackexchange.com/questions/7870/how-to-check-how-long-a-process-has-been-running/7871
[3] stackoverflow.com/questions/385408/get-program-execution-time-in-the-shell

Image Credit: target.com

More Reading:


Leave a Reply

Your email address will not be published. Required fields are marked *