How do you kill all Linux processes that are older than a certain age? How do you kill all Linux processes that are older than a certain age? unix unix

How do you kill all Linux processes that are older than a certain age?


Found an answer that works for me:

warning: this will find and kill long running processes

ps -eo uid,pid,etime | egrep '^ *user-id' | egrep ' ([0-9]+-)?([0-9]{2}:?){3}' | awk '{print $2}' | xargs -I{} kill {}

(Where user-id is a specific user's ID with long-running processes.)

The second regular expression matches the a time that has an optional days figure, followed by an hour, minute, and second component, and so is at least one hour in length.


If they just need to be killed:

if [[ "$(uname)" = "Linux" ]];then killall --older-than 1h someprocessname;fi

If you want to see what it's matching

if [[ "$(uname)" = "Linux" ]];then killall -i --older-than 1h someprocessname;fi

The -i flag will prompt you with yes/no for each process match.


For anything older than one day,

ps aux

will give you the answer, but it drops down to day-precision which might not be as useful.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMANDroot         1  0.0  0.0   7200   308 ?        Ss   Jun22   0:02 init [5]root         2  0.0  0.0      0     0 ?        S    Jun22   0:02 [migration/0]root         3  0.0  0.0      0     0 ?        SN   Jun22   0:18 [ksoftirqd/0]root         4  0.0  0.0      0     0 ?        S    Jun22   0:00 [watchdog/0]

If you're on linux or another system with the /proc filesystem, In this example, you can only see that process 1 has been running since June 22, but no indication of the time it was started.

stat /proc/<pid>

will give you a more precise answer. For example, here's an exact timestamp for process 1, which ps shows only as Jun22:

ohm ~$ stat /proc/1  File: `/proc/1'  Size: 0               Blocks: 0          IO Block: 4096   directoryDevice: 3h/3d   Inode: 65538       Links: 5Access: (0555/dr-xr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2008-06-22 15:37:44.347627750 -0700Modify: 2008-06-22 15:37:44.347627750 -0700Change: 2008-06-22 15:37:44.347627750 -0700