DZone
Performance Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > How to Get the Linux Process Execution Time When You've Already Started

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?

Denny Zhang user avatar by
Denny Zhang
·
Apr. 03, 17 · Performance Zone · Tutorial
Like (2)
Save
Tweet
12.82K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Execution (computing) File system Linux (operating system)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • Purpose-Driven Microservice Design
  • Refactor Switch to a One-Liner
  • Architecture as Code With C4 and Plantuml

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo