Figuring out how long a Linux process has been alive


I’ve bumped into a few problems in the past where processes that were supposed to be short lived encountered an issue and never died. Over time these processes would build up and if it wasn’t for a cleanup task I developed the process table would have eventually filled up (the bug that caused this was eventually fixed).

Now how would you go about checking to see how long a process has been alive? There are actually several ways to get the time a process started on a Linux host. You can look at the 5th field in the SYSV ps output:

$ ps -ef | tail -5

matty 29501 28486 0 Nov02 pts/6 00:00:00 ssh 192.168.56.101
matty 29666 28085 0 Nov02 pts/7 00:00:00 bash
matty 29680 29666 0 Nov02 pts/7 00:00:00 vim
root 29854 2 0 Oct31 ? 00:00:00 [kdmflush]
matty 29986 20521 0 Nov02 ? 00:00:07 java

You can get a space separate date range with the “bsdstart” option:

$ ps ax -o pid,command,bsdstart,bsdtime | tail -5

29501 ssh 192.168.56.101 Nov 2 0:00
29666 bash Nov 2 0:00
29680 vim Nov 2 0:00
29854 [kdmflush] Oct 31 0:00
29986 java Nov 2 0:07

Or you can get the full date a process started with the “lstart” option:

$ ps ax -o pid,command,lstart | tail -5

29501 ssh 192.168.56.101 Wed Nov 2 14:16:23 2011
29666 bash Wed Nov 2 14:56:48 2011
29680 vim Wed Nov 2 14:56:49 2011
29854 [kdmflush] Mon Oct 31 10:56:54 2011
29986 java Wed Nov 2 15:54:05 2011

Now you may be asking yourself where does ps get the time from? It opens /proc/ /status and reads the starttime value to get the time in jiffies that the process was started after boot (see proc(5) for further detail). I’m sure there are numerous other ways to visualize the time a process has been running, but the ones listed above have been sufficient to deal with most aging issues (aka. bugs) I’ve encountered. :)

This article was posted by Matty on 2011-11-06 08:10:00 -0400 -0400