Home Linux Tips & Tricks How To Find Out How Long A Process Has Been Running In Linux

How To Find Out How Long A Process Has Been Running In Linux

By sk
Published: Last Updated on 11.3K views

Have you ever been in a situation where you wanted to know how long a process has been running on your Linux box? No? No problem! This brief guide helps you to find out the uptime of an active process in Linux.

You don't need any monitoring applications. In Linux and other Unix-like operating systems, there is a command called ps, which is used to display the information about the active processes. Using ps command, we can easily find out how long a process is running in Linux.

Find out how long a process has been running in Linux

The ps command has different format specifiers (keywords) that can be used to control the output format. We are going to use the following two keywords to find the uptime of an active process.

  • etime - elapsed time since the process was started, in the form of [[DD-]hh:]mm:ss.
  • etimes - elapsed time since the process was started, in seconds.

First, you need to find out the PID of a process. The following command displays the PID of dhcpcd process.

$ pidof dhcpcd
8299

As you see in the above output, 8299 is the PID of dhcpcd process.

Now, we can find how long this process has been running using command:

$ ps -p 8299 -o etime
 ELAPSED
 04:05:37

You can also view the elapsed time in seconds using etimes keyword.

$ ps -p 8299 -o etimes
ELAPSED
 14749
Find out how long a process has been running in Linux using ps command
Find out how long a process has been running in Linux using ps command

Not only a single process, we can also display the uptime of all processes like below.

$ ps -eo pid,comm,lstart,etime,time,args

Or,

$ ps -eo pid,comm,lstart,etimes,time,args

The first command displays the uptime of all Linux processes, in [[DD-]hh:]mm:ss format, and the latter displays the uptime in seconds.

Here is the sample output of second command.

Find uptime of all processes using ps command in Linux
Find uptime of all processes using ps command in Linux

As you see in the above output, we have the uptime of all processes with six columns format.

Here,

  • PID - The Process ID.
  • COMMAND (second column) - The command name without options and/or arguments.
  • STARTED - The absolute starting time of the process.
  • ELAPSED - The elapsed time since the process was started, in the form of [[dd-]hh:]mm:ss.
  • TIME - Cumulative CPU time, "[dd-]hh:mm:ss" format.
  • COMMAND (last column) - Command name with all its provided options and arguments.

For more details about ps command, check the man pages.

$ man ps

You May Also Like

2 comments

jhuebel July 15, 2017 - 1:27 am

This bash script will return the elapsed time (in seconds) of any processes with the specified name, without any “ELAPSED” text or whitespace. If there are multiple processes, then the times for each process are seperated by newlines.

Paste this into a file called piduptime.sh and make it executable. Syntax is “piduptime.sh &ltprocess name&gt”.

#!/bin/bash

PIDS=”$(pidof $1)”
for PID in $PIDS
do
ps -o etimes -p $PID | sed -n 2p | sed ‘s/ //g’
done

Reply
SK July 15, 2017 - 11:39 am

Thank you. I will check it out.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More