Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a problem with some zombie-like processes on a certain server that need to be killed every now and then. How can I best identify the ones that have run for longer than an hour or so?

like image 526
yukondude Avatar asked Aug 08 '08 16:08

yukondude


People also ask

What is kill all command in Linux?

When started by a root user, the killall command cancels all cancellable processes except those processes that started it. If several Signals are specified, only the last one is effective. If no signal is specified, the killall command sends a SIGKILL signal.

How do I kill all user processes?

How to kill all user processes with killall. Killall command allows you to terminate all the processes owned by a specific user. To do this, use the -u flag. For example, to terminate all processes spawned by the ubuntu user.


2 Answers

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.

like image 96
yukondude Avatar answered Oct 10 '22 14:10

yukondude


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.

like image 22
Jodie C Avatar answered Oct 10 '22 14:10

Jodie C