Have you ever started a script that needs to run for an extended period of time? Maybe you kicked off a job and it is still running next time you log in? Whatever the situation is, there may be times when it is necessary to check how long a process has been running in Linux. In this short tutorial we will discuss using the ps command to show elapsed time since a process was started.

Every time you start a process on a Linux system it is assigned a process id (PID). The system keeps track of this process, it's elapsed time, and other important information using this process id. Before we can find out how long a process has been running we need to find its PID.

Find PID of a Process

There are several ways to find the PID of a process. In this example we will look for the PID of Plex Media Server using the pidof command. Simply issue the command followed by the name of the process for which you want the PID.

[mcherisi@Fenrir ~]$ pidof "Plex Media Server"
10072

As you can the PID for the process named "Plex Media Server" is returned.

NOTE: In this case the quotes around the name are necessary since the name of the process has spaces. For more information read "Using Filenames with Spaces or Special Characters in Linux".

Now that we have the PID of the process, we can use this information to find the elapsed time since the process was started.

Check How Long a Process Has Been Running (Elapsed Time)

Now that we have the process ID, we can use the ps command with some options to find the elapsed time since it was started. There are two main options to the ps command we will be using here. First we will pass the -p to identify the process we want information about. The next option is -o etime which will allow us to format the output and view only the elapsed time.

Here is an example of how to check the elapsed time since the Plex Media Server process was started.

[mcherisi@Fenrir ~]$ ps -p 10072 -o etime
    ELAPSED
   02:20:56

Using etime allows us to see the elapsed time in Day-Hour:Minute:Second format. Here we see the process has been running for 2 hours, 20 minutes, and 56 seconds. Here is another example using the PID of the init process (first process launched by the system).

[mcherisi@Fenrir ~]$ ps -p 1 -o etime
    ELAPSED
 7-15:44:59

In the above example we can see the process has been running for 7 day, 15 hours, 44 minutes, and 59 seconds.

We can use -o etimes to see how long a process has been running formatted in seconds.

[mcherisi@Fenrir ~]$ ps -p 1 -o etimes
ELAPSED
 661204

Of course a seasoned Linux admin could use command substitution to get this done in a single command:

[mcherisi@Fenrir ~]$ ps -p $(pidof "Plex Media Server") -o etime
    ELAPSED
   02:26:40

That's it! You should now know how to easily check how long a process has been running in Linux. For more information on the ps command see the links in the resources below.