Linux: Find out how long a process has been running

If you need to figure out how long a process has been running (e.g. to identify long running processes or just monitor some processes), you can use the -o etime option of the ps command. Here the description from the ps manual:

etime elapsed time since the process was started, in the form [[dd-]hh:]mm:ss.

If you want to monitor a given process (or in my case multiple processes started with the same command), you can combine it with -o command which will output the command line used to start the process and grep it:

# ps -e -o command,etime | grep scriptpool.sh | grep -v grep
/bin/sh ./scriptpool.sh 1         09:51
/bin/sh ./scriptpool.sh 2         09:51
/bin/sh ./scriptpool.sh 3         09:51

The information about elapsed time comes from /proc/<pid>/stat but it is here displayed in a human readable form. Anyway from /proc/<pid>/stat you’d get a number of jiffies and need to find out how to convert it to something more understandable… So let’s stick to ps…

If you know the pid of the process it’s even easier, you can just use the -p option of ps instead of grep:

# ps -o command,etime -p 7272
COMMAND                         ELAPSED
/bin/sh ./scriptpool.sh 1         21:42

Here you need to remove the -e option otherwise it will override the -p option and will display all processes.

Email this to someoneShare on FacebookShare on Google+Share on LinkedInDigg thisShare on RedditShare on StumbleUponTweet about this on TwitterBuffer this page

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>