How to check how long a process ran after it finished?

Currently I’m using the following to check how long a process is actually running:

ps -eo uid,pid,etime | egrep '^ *MY_ID' | egrep 'PID_OF_PROCESS'

And that outputs the following:

MY_ID PID_OF_PROCESS       00:16

However, after the process ends I want to find out how long it actually ran for but I can’t seem to find that information.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I don’t think it is possible to obtain such information after the process has already finished.

If you know beforehand that you will need that information, you can run the command as

time <command>

e.g.

~> time sleep 1

real        0m1.003s
user        0m0.002s
sys         0m0.001s

Method 2

Check if your unix variant supports process accounting. For example, on Ubuntu (and most other Linux distributions), this is provided by the acct package Install acct http://bit.ly/software-small. If the accounting subsystem is up and running, then lastcomm shows information about finished processes, including how much processor time they used (the wall clock time isn’t recording).

If you need timing for a specific program, invoke it through time.

If you need more precise information, use a fancier auditing package, for example the audit subsystem under Linux.

Method 3

Before I saw this questions and its answers, I made a little bash script. This is still useful, if you don’t have root rights and acct isn’t installed or you don’t invoke your command directly (in my case: I use a gui to setup background processes). You need to get the PID before running (anyone interested in adding it into the script? 🙂

#!/bin/bash

echo "Checking run time"
read -p "What is the pid? " PID

while true; do
    # sleep needed! used to reduce cpu usage and mass of output
    sleep 5
    ps -eo uid,pid,etime | egrep '$PID' | egrep '$UID'
done

With this, find the PID of PROCESS_NAME

ps aux | awk 'NR == 1 || /PROCESS_NAME/'

Actually I tried combining USER-ID and PID to make the script non-ambigious, but it’s not that easy…
ps -eo uid,pid,etime | egrep '$UID[[:space:]]$PID' don’t seem to work always…


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x