List processes that have been running more than 2 hours

5,793

One liner to find processes that have been running for over 2 hours

ps -e -o "pid,etimes,command" | awk '{if($2>7200) print $0}'

Explanation:

ps: process snapshot command

-e: list all processes

-o: include only specified columns

pid: process id

etimes: elapsed time since the process was started, in seconds

command: command with all its arguments as a string

awk: pattern scanning and processing language

$2: second token from each line (default separator is space)

7200: 7200 seconds = 2 hours

$0: the whole line in awk

More:

man ps
man awk
Share:
5,793

Related videos on Youtube

Vinod
Author by

Vinod

Updated on September 18, 2022

Comments

  • Vinod
    Vinod almost 2 years

    How can I list processes, with a defined name, that have been running for more than 2 hours. This is what I have tried.

    ps -efo pid,comm,etime | grep 'process name' | awk '{print $3}' 
    

    This is for Solaris.

    Or can someone help how to create a script that will send an email with the process IDs if there are processes running longer than 2 hours.

    • Andrew Henle
      Andrew Henle almost 5 years
      What do you mean by "longer than 2 hours"? Wall-clock time since the process was started? CPU time consumed?
    • Andrew Henle
      Andrew Henle almost 5 years
      The modification time of /proc/[PID] is going to be the time the process started. I tried to see if some variation of find /proc \! -mmin 120 ... would work, but wasn't able to come up with a proper find command that would limit the depth of the search to just the /proc/[PID] level in the limited time I had. Someone with better skill with find can probably solve the problem easily. Note that you will pick up a lot of OS processes that you really don't want to kill.
  • L. Scott Johnson
    L. Scott Johnson almost 5 years
    The "something you wrote" is missing?
  • Mark Stewart
    Mark Stewart almost 5 years
    Yes I had serious formatting issues when I tried to post code. Cleaned up and now posted. :D