Kill process if CPU usage above 80% for more than 1:30
-
dandsk
- A regular

- Posts: 33
- https://www.youtube.com/channel/UC40BgXanDqOYoVCYFDSTfHA
- Joined: Sat Mar 29, 2014 6:15 pm
Kill process if CPU usage above 80% for more than 1:30
Hello,
Sometimes when a game server crashes on one of my VDS's it starts hogging the CPU core it was set to use. This does not effect the other game servers on the server as they run on separate cores, but in order for the game server to appear back online I have to manually kill the process and force a restart. These specific crashes rarely happen, sometimes once per 2 weeks, but still is a pain to manually force restarts.
I was wondering if anybody could provide me a script that would monitor processes and detect if a process hits above 80% CPU and stays above 80% for more than 1 minute and 30 seconds.
Thanks.
Sometimes when a game server crashes on one of my VDS's it starts hogging the CPU core it was set to use. This does not effect the other game servers on the server as they run on separate cores, but in order for the game server to appear back online I have to manually kill the process and force a restart. These specific crashes rarely happen, sometimes once per 2 weeks, but still is a pain to manually force restarts.
I was wondering if anybody could provide me a script that would monitor processes and detect if a process hits above 80% CPU and stays above 80% for more than 1 minute and 30 seconds.
Thanks.
Re: Kill process if CPU usage above 80% for more than 1:30
Edit:
"I was wondering if anybody could provide me a script that would monitor processes and detect if a process hits above 80% CPU and stays above 80% for more than 1 minute and 30 seconds."
Change that to:
I was wondering if anybody could provide me a script that would monitor processes and detect if a process hits above 80% CPU and stays above 80% for more than 1 minute and 30 seconds, then kill it.
"I was wondering if anybody could provide me a script that would monitor processes and detect if a process hits above 80% CPU and stays above 80% for more than 1 minute and 30 seconds."
Change that to:
I was wondering if anybody could provide me a script that would monitor processes and detect if a process hits above 80% CPU and stays above 80% for more than 1 minute and 30 seconds, then kill it.
Re: Kill process if CPU usage above 80% for more than 1:30
Nobody knows? 
Quick Google search and came up with these, but I'm unsure of how to edit and get the following scripts working:
ps --user www-data -o pid,time,pcpu|awk '{
if($3 > 70) {
system("kill "$1);
}
}'
and
http://www.perlmonks.org/?node_id=647154
Quick Google search and came up with these, but I'm unsure of how to edit and get the following scripts working:
ps --user www-data -o pid,time,pcpu|awk '{
if($3 > 70) {
system("kill "$1);
}
}'
and
http://www.perlmonks.org/?node_id=647154
Re: Kill process if CPU usage above 80% for more than 1:30
With a home-rolled script, you could do this by running "top" with a 1'30" interval and then parsing the CPU usage column for each process.
For a 3rd party solution, you might consider using "monit" -- I haven't experimented with it personally, but I hear good things.
For a 3rd party solution, you might consider using "monit" -- I haven't experimented with it personally, but I hear good things.
Re: Kill process if CPU usage above 80% for more than 1:30
If you decide to use Monit, make sure you compile it yourself, as the binary is using a vulnerable version of OpenSSL at this time.
Re: Kill process if CPU usage above 80% for more than 1:30
Hello, thanks for replying.
I don't want to use third party programs, just a simple script to detect when a process hits 80% CPU or more for more than 1 minute and 30 seconds.
Found this page:
http://stackoverflow.com/questions/1846 ... n-on-linux
And found a simple script:
ps aux | grep 'gnome-panel ' | awk '{if ($3>80)print $2}' | xargs kill -9
Would I simply do: ps aux | grep 'srcds_linux' | awk '{if ($3>80)print $2}' | xargs kill -9 ?
Thanks.
I don't want to use third party programs, just a simple script to detect when a process hits 80% CPU or more for more than 1 minute and 30 seconds.
Found this page:
http://stackoverflow.com/questions/1846 ... n-on-linux
And found a simple script:
ps aux | grep 'gnome-panel ' | awk '{if ($3>80)print $2}' | xargs kill -9
Would I simply do: ps aux | grep 'srcds_linux' | awk '{if ($3>80)print $2}' | xargs kill -9 ?
Thanks.
Re: Kill process if CPU usage above 80% for more than 1:30
No, that wouldn't work. With "ps", you get the total CPU usage for the process over its lifetime and not over an arbitrary period of time.
Like "ps", "top" is included with all Linux distributions, so you won't have to install it separately.
Like "ps", "top" is included with all Linux distributions, so you won't have to install it separately.
Re: Kill process if CPU usage above 80% for more than 1:30
Oh, sorry. I was in a hurry at the time of that posting and just quickly retrieved some code of a forum post. Needed a response fast...
Anyways, took some time to look around and found this post: http://stackoverflow.com/questions/1846 ... n-on-linux
The second post looks like what I am looking for. I plan to set it up by the end of tomorrow.
Anyways, took some time to look around and found this post: http://stackoverflow.com/questions/1846 ... n-on-linux
The second post looks like what I am looking for. I plan to set it up by the end of tomorrow.
Re: Kill process if CPU usage above 80% for more than 1:30
Right, but you were suggesting that you didn't want to install extra 3rd party libraries. The "top" method would be best if you don't.
Re: Kill process if CPU usage above 80% for more than 1:30
What you're suggesting is good, but I have no knowledge on how to exactly "make" the script.
The second post on this http://stackoverflow.com/questions/1846 ... n-on-linux seems okay to go with, but if anyone can make or provide a script which is similar to what I am looking for I will forever be grateful!
The second post on this http://stackoverflow.com/questions/1846 ... n-on-linux seems okay to go with, but if anyone can make or provide a script which is similar to what I am looking for I will forever be grateful!

