LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-24-2012, 02:35 PM   #1
dmaksimov
LQ Newbie
 
Registered: Mar 2007
Posts: 4

Rep: Reputation: 0
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.
 
Old 03-24-2012, 04:44 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You should be able to do that w/ cron and a script ....
 
Old 03-24-2012, 05:48 PM   #3
dmaksimov
LQ Newbie
 
Registered: Mar 2007
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 03-24-2012, 05:59 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 03-24-2012, 08:50 PM   #5
dmaksimov
LQ Newbie
 
Registered: Mar 2007
Posts: 4

Original Poster
Rep: Reputation: 0
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?
 
Old 03-24-2012, 09:36 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,533

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Maybe you should change your test to match the data ?. Remembering it's a string of course ...
 
Old 03-25-2012, 01:51 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by dmaksimov View Post
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
 
Old 03-25-2012, 02:06 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,533

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Personally, I wouldn't bother splitting it - just test as-is.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CPU usage by processes using top command mrme Linux - Software 3 02-24-2012 10:36 PM
Kill process with high CPU usage after X time? touser Linux - Newbie 4 12-27-2009 09:52 PM
processes/CPU Usage/Mem Usage desktop wallpaper ceantuco Linux - Newbie 2 04-13-2009 01:14 PM
kill -9 pid: 10 cpu usage 97.6% aquaboot *BSD 7 01-04-2008 01:33 PM
How can I capping the CPU usage of processes ideasman Linux - Software 0 04-01-2004 03:52 AM


All times are GMT -5. The time now is 12:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration