Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Get the Linux Process Execution Time When You've Already Started

DZone's Guide to

How to Get the Linux Process Execution Time When You've Already Started

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?

· Performance Zone
Free Resource

Transform incident management with machine learning and analytics to help you maintain optimal performance and availability while keeping pace with the growing demands of digital business with this eBook, brought to you in partnership with BMC.

Say you have issued a command in your servers. Typically, the command might either back up something or perform a critical hotfix.

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?

Getting the start time of a running process is easy. Either use the ps CLI or query the /proc file system directly.

Via ps command line: 

# Show process start time by pid
ps -o lstart= -p $pid

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

Via the /proc file system directly:

awk '{print "CPU time: " $14+$15; print "start time: " $22}' \
  /proc/$pid/stat

Getting the process execution time is  if we can control how it is started. Simply use time $command.

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 and you have no estimation 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. But a pro wouldn’t do that. That's pretty boring and time-wasting.

We all know the watch command, right?

The 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 log file, we can get:

  • The process starting time with the first entry of the log file.
  • The ending time of 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, isn’t it?

Any further improvements?

You can get a timely notification when the process ends. The process could have been done for several hours before you even remember to log in and check the status via the log file.

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

# 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"


Then we’re all set!

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Topics:
linux ,performance ,monitoring ,tutorial

Published at DZone with permission of Denny Zhang, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}