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
Related videos on Youtube
Author by
Vinod
Updated on September 18, 2022Comments
-
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 almost 5 yearsWhat do you mean by "longer than 2 hours"? Wall-clock time since the process was started? CPU time consumed?
-
Andrew Henle almost 5 yearsThe modification time of
/proc/[PID]
is going to be the time the process started. I tried to see if some variation offind /proc \! -mmin 120 ...
would work, but wasn't able to come up with a properfind
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 withfind
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 almost 5 yearsThe "something you wrote" is missing?
-
Mark Stewart almost 5 yearsYes I had serious formatting issues when I tried to post code. Cleaned up and now posted. :D