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 any amount of whitespace)
7200
: 7200 seconds = 2 hours
$0
: the whole line in awk
Since the default action in the pattern { action }
structure in awk is to print the current line, this can be shortened to:
ps -e -o pid,etimes,command | awk '$2 > 7200'
More:
man ps
man awk
/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.