HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
Secure OS Software for Linux
cancel
Showing results for 
Search instead for 
Did you mean: 

Script for monitoring hang process

 
SOLVED
Go to solution
bong_3
Advisor

Script for monitoring hang process

Hi all,

I just started to read/learn scripting for linux and find it very interesting. Now i'm trying to monitor a process that always hangup on my linux and perhaps kill it by running a script under cron.

see my attachment...

from there, when i run a top command it shows me that bash process under user1 takes up a lot of process which causes my linux hang.

I wonder whether anyone can suggest how to write a script for this or perhaps share your scripts if happens that you already have one.

Appreciate anyones help.

Bong
6 REPLIES
Derek Whigham_2
Occasional Advisor

Re: Script for monitoring hang process

ps -efd|grep |awk '{print $2}'|xargs -x kill -9

This should kill the first instance of any entered

Derek
Mark Grant
Honored Contributor

Re: Script for monitoring hang process

You have two problems here. The first is that it is extremely unusual for "bash" to be taking up all your CPU unless you are running some script stuck in a loop that doesn sleep or wait or something. Secondly, how are you going to determin that a process has hung. My personal favourite for determining a process that definately isn't very well is to check the "C" field as determined by "ps" and look for 255 as a consistant value.

Rather than kill off hung processes, I would suggest it is more important to determine why the thing is hanging or hogging CPU in the first place. I would be interested in your example to know exactly what "user1" is doing.
Never preceed any demonstration with anything more predictive than "watch this"
Stuart Browne
Honored Contributor

Re: Script for monitoring hang process

I'm with Mark's school of thinking here.

Don't write stop-gag's to get *around* issues.. Fix the issue.
One long-haired git at your service...
bong_3
Advisor

Re: Script for monitoring hang process

Hi,

Agree with both Mark & Stuart, of course I need to find out what causing this bash hang. But as of the moment, at least make a temporary solution for this.

Bong
Stuart Browne
Honored Contributor
Solution

Re: Script for monitoring hang process

As Mark states, you need to watch the CPU usage.

Using the 6th column (C) in the output of 'ps -elf' is by var the easiest way to go.

When the system is in it's bad state, check tos ee what the 'C' column is doing in a ps output.

To continually monitor it, a simple routine similar to this should suffice:

while sleep 5
do
ps -elf | awk '{
if ($6 > 250) {
system("kill -HUP " $4);
}
}'
done

The two arbitary numbers are the 5 in the 'sleep' (how long between checks), and the 250 in in the awk's if statement (whatever is a bad value from your ps checks earlier).

.. just an idea atleast ..
One long-haired git at your service...
Ian Kidd_1
Trusted Contributor

Re: Script for monitoring hang process

Like the majority, I'm in favor of finding out what's causing bash to run at such a high CPU%.

The next time you see the problem, try using `ps -ef` and follow the parent/child process numbers (or see if `pstree` can give you enough info) to see what's causing this problem.

You could write a script to kill a bash shell if it reaches a certain percentage value, but you run the danger of killing something you might not have wanted to be killed.
If at first you don't succeed, go to the ITRC