DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Personalized Code Searches Using OpenGrok
  • DevOps in Legacy Systems
  • Observability Architecture: Financial Payments Introduction
  • IDE Changing as Fast as Cloud Native

Trending

  • Personalized Code Searches Using OpenGrok
  • DevOps in Legacy Systems
  • Observability Architecture: Financial Payments Introduction
  • IDE Changing as Fast as Cloud Native
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. 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 · Tutorial
Like (2)
Save
Tweet
Share
13.42K 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.

Trending

  • Personalized Code Searches Using OpenGrok
  • DevOps in Legacy Systems
  • Observability Architecture: Financial Payments Introduction
  • IDE Changing as Fast as Cloud Native

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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

Let's be friends: