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 | grep -v grep
/bin/sh ./ 1         09:51
/bin/sh ./ 2         09:51
/bin/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 ./ 1         21:42

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

Leave a Reply

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