I always forget the ps command arguments to see the length of time that a Linux process has been running so I am writing it out here in hopes of remembering it in the future without having to read the ps manpage. I also will know I can easily search this later to find the solution. Below I describe what command to use from a Linux terminal window to display the length of time a specific process or set of processes has been running.
List Process Name & Time It Has Been Running On Linux:
In the examples below I am using CentOS Linux however the command should be the same across Linux distributions. Typically when I issue the ps command I use the -e and -f switches for a detailed output of all of the processes running on a server. If I need to narrow down the output I grep something like httpd or whatever processes I am looking into. There are other times that come up when I am trying to calculate the length of time that a process has been running and I don’t feel like checking the current date/time and subtracting the start time of the process as displayed when issuing “ps -ef”. In this second scenario ps is still the perfect tool but only when used with different switches/variables. For this example I am attempting to find out the length of time that a shell script I launched from the command line called snapshot_walkint.sh.
Example Linux Process Length Of Time Running Output:
[root@dev ~]# ps -eo comm,etime | grep snapshot snapshot_walkin 03:12:50 [root@dev ~]#
As you can see in the above ouput the process has been running for three hours, twelve minutes, and fifty seconds. You could calculate the same using the ps -ef | grep snapshot command however you would then have to subtract the process start date/time from the server date/time as shown below.
More Complex Way To Obtain Process Run Time On Linux:
[root@dev ~]# ps -ef | grep snapshot root 22055 12634 0 05:15 pts/3 00:00:00 /bin/sh /usr/local/bin/snapshot_walkint.sh root 23564 3325 0 08:14 pts/1 00:00:00 grep snap [root@dev ~]# [root@dev ~]# date Sun Jul 15 08:14:45 PDT 2012 [root@dev ~]#
In this second example you would subtract 05:15 from 08:14:45 and come up with two hours fifty nine minutes and fifteen seconds. Notice this isn’t as exact because seconds are not included in the output of “ps -ef”. Using the first example again you might be troubleshooting a list of processes such as mingetty as shown in the below example. You might be looking for one mingetty process that hasn’t been functioning properly because it was started long before the rest of the processes.
List Specific Linux Processes & The Amount Of Time They Have Been Running:
[root@dev ~]# ps -eo comm,etime | grep mingetty mingetty 04:49:51 mingetty 04:49:51 mingetty 04:49:51 mingetty 04:49:51 mingetty 04:49:51 mingetty 04:45:27 [root@dev ~]#
Knowing the specific amount of time a process has been running is useful in many ways and now I will remember the location of this article if I have need to reference. See the ps manpage for more variables you can display in the ps command output in whatever order you choose.