LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Kill processes which take up too much cpu usage after certain time of running. (https://www.linuxquestions.org/questions/linux-newbie-8/kill-processes-which-take-up-too-much-cpu-usage-after-certain-time-of-running-936223/)

dmaksimov 03-24-2012 02:35 PM

Kill processes which take up too much cpu usage after certain time of running.
 
Hello,

I'm running an Ubuntu server with Apache and every once in a while I get processes that hang at CLOSE_WAIT. The only solution I've found is to just kill the process. Instead of doing this manually is there a way to kill the processes that hang automatically, with like pkill or something.

Something that looks for a process that has taken up more than lets say 75% of the cpu for at least 1 minute or so.

Thanks in advance.

Tinkster 03-24-2012 04:44 PM

You should be able to do that w/ cron and a script ....

dmaksimov 03-24-2012 05:48 PM

Any idea on how I can go about doing this?

I was looking through /proc/PID/stat to see if I can calculate the cpu usage and execution time.

Tinkster 03-24-2012 05:59 PM

Pseudo-code following...

Something like:
Code:

for i in $(netstat -tpn|awk -F/ '/apache/ && /CLOSE_WAIT/{print $1})
do
  ps -eo pid,time,pcpu|awk '$1 == $1 && $2 > <timelimit> && $3 > <cpu> {"kill "$1}'>/dev/null 2>&1
done


dmaksimov 03-24-2012 08:50 PM

Thanks that helped a lot! :)
I modified it a bit and got this:
Code:

ps --user www-data -o pid,time,pcpu|awk '{
if($3 > 70) {
system("kill "$1);
}
}'

But time is in the format 00:12:32, how can I test if it's greater than?

syg00 03-24-2012 09:36 PM

Maybe you should change your test to match the data ?. Remembering it's a string of course ...

Tinkster 03-25-2012 01:51 AM

Quote:

Originally Posted by dmaksimov (Post 4635487)
Thanks that helped a lot! :)
I modified it a bit and got this:
Code:

ps --user www-data -o pid,time,pcpu|awk '{
if($3 > 70) {
system("kill "$1);
}
}'

But time is in the format 00:12:32, how can I test if it's greater than?


Does this help?
Code:

$ echo a b 00:12:34 | awk '{split($3,a, ":");print a[1]*60*60+ a[2]*60 + a[3]}'
754


syg00 03-25-2012 02:06 AM

Personally, I wouldn't bother splitting it - just test as-is.


All times are GMT -5. The time now is 03:39 PM.