Kill process if CPU usage above 80% for more than 1:30

Ask questions about dedicated servers here and we and other users will do our best to answer them. Please also refer to the self-help section for tutorials and answers to the most commonly asked questions.
Post Reply
dandsk
A regular
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

Post by dandsk »

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.
dandsk
A regular
A regular
Posts: 33
Joined: Sat Mar 29, 2014 6:15 pm

Re: Kill process if CPU usage above 80% for more than 1:30

Post by dandsk »

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.
dandsk
A regular
A regular
Posts: 33
Joined: Sat Mar 29, 2014 6:15 pm

Re: Kill process if CPU usage above 80% for more than 1:30

Post by dandsk »

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
User avatar
Edge100x
Founder
Founder
Posts: 13156
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Kill process if CPU usage above 80% for more than 1:30

Post by Edge100x »

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.
joshh20
This is my homepage
This is my homepage
Posts: 146
Joined: Sat Mar 02, 2013 1:35 pm

Re: Kill process if CPU usage above 80% for more than 1:30

Post by joshh20 »

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.
dandsk
A regular
A regular
Posts: 33
Joined: Sat Mar 29, 2014 6:15 pm

Re: Kill process if CPU usage above 80% for more than 1:30

Post by dandsk »

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.
User avatar
Edge100x
Founder
Founder
Posts: 13156
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Kill process if CPU usage above 80% for more than 1:30

Post by Edge100x »

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.
dandsk
A regular
A regular
Posts: 33
Joined: Sat Mar 29, 2014 6:15 pm

Re: Kill process if CPU usage above 80% for more than 1:30

Post by dandsk »

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.
User avatar
Edge100x
Founder
Founder
Posts: 13156
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Kill process if CPU usage above 80% for more than 1:30

Post by Edge100x »

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.
dandsk
A regular
A regular
Posts: 33
Joined: Sat Mar 29, 2014 6:15 pm

Re: Kill process if CPU usage above 80% for more than 1:30

Post by dandsk »

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!
Post Reply