PDA

View Full Version : req: Bash / Script to auto kill PID if it's needed....


edge
12th March 2006, 00:44
Hello nice people from HowtoForge :-)

I'm really in need of a script (bash will do) to kill a PID/USER when it's taking 99% of %CPU power for over a set time!

I was looking of a way of doing this with some use of top

When running top like this: top -1 -n 1 -b -u coldfusion it will show only the Coldfusion PID/USER once.
The PID/USER that is taking 99% of cpu power will also sit at the top of the list.


top -1 -n 1 -b -u coldfusion
top - 23:35:47 up 1 day, 1:37, 2 users, load average: 2.74, 2.22, 2.24
Tasks: 243 total, 5 running, 238 sleeping, 0 stopped, 0 zombie
Cpu0 : 17.6% us, 11.3% sy, 0.0% ni, 71.1% id, 0.0% wa, 0.0% hi, 0.0% si
Cpu1 : 20.0% us, 11.4% sy, 0.0% ni, 68.6% id, 0.0% wa, 0.0% hi, 0.0% si
Cpu2 : 20.3% us, 9.0% sy, 0.0% ni, 70.5% id, 0.2% wa, 0.0% hi, 0.0% si
Cpu3 : 19.4% us, 9.0% sy, 0.0% ni, 70.8% id, 0.4% wa, 0.0% hi, 0.3% si
Mem: 2068288k total, 1868620k used, 199668k free, 120004k buffers
Swap: 787176k total, 52k used, 787124k free, 1034136k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2934 coldfusi 18 0 21812 6396 3300 R 99.4 0.3 0:00.11 convert
8351 coldfusi 16 0 880m 506m 26m R 48.7 25.1 26:09.50 cfmx7
30206 coldfusi 16 0 880m 506m 26m S 17.5 25.1 0:04.90 cfmx7
26654 coldfusi 15 0 880m 506m 26m S 9.7 25.1 15:55.54 cfmx7
31087 coldfusi 15 0 880m 506m 26m S 9.7 25.1 9:37.35 cfmx7

(as you can see, the PID 2934 (user coldfusi) is taking with the command convert over 99% of CPU pwrd)

What I'm after is some script (bash?) that I can run on a cron job every 5 / 10 minutes to see if coldfusi with the command convert is sitting at or over 99%, and if so to auto kill the PID!

Anyone here who can help/show me how to make this?

Thank you...

Noel

falko
12th March 2006, 16:02
You could parse the output of ps aux

edge
12th March 2006, 18:02
You could parse the output of ps aux

falko.. you again :-) .. you are my nr1 person here :-)

The ps aux does look a usable tool..
The only thing is that I can not find a way to sort the result by CPU use.


********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full --Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra full X registers --heading --no-heading --context
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy

It does not really give a 'sort by CPU use' option... Is there a hidden option for this?

falko
12th March 2006, 20:13
Why don't you use something like
ps aux|grep coldfusion|grep -v grep to find all processes related to coldfusion?

edge
12th March 2006, 22:05
Okay.. getting close now..

I found some bash code that might do the trick for me..
Only problem.. I'm getting a syntax error: unexpected end of file (on line 16)

The code will dump the 'ps auxww' output for 'convert' in a file named 'rmp-grep'
Than it will read and 'grep' the file. If $2 is greater than (in this example) 20 it should show BAD Process found

Some one here (falko) who can have a look and let me know why it's giving the 'unexpected end of file' error?

** edit..
When it does find the process and when it's above 20% it will give this error:
./test: line 10: [: 0.0: integer expression expected (I've named the script as 'test' for now)

Thank you


#!/bin/bash
#
ps auxww | grep "cfmx7" | grep -v grep | cut -c10-14,15-20,61- > tmp-grep
export LINE
(
read LINE
while [ -n "$LINE" ]
do
set $LINE
if [ $2 -gt 20 ]; then
echo "BAD Process found";
EOF
fi
read LINE
done
)< tmp-grep

edge
13th March 2006, 14:46
Okay.. I got it :-)

The scrip will email me when cfmx7 is using more than 90% CPU pwr


#!/bin/bash

# March-13-2006
# CPUuse trigger script by Noel
#
# bash code to watch a running program's CPU usage.
# if it's above a set value, it will auto send an email.
# You will need to set a Cron job to run this script every xx minutes
#
# Set some needed things:
#
processToWatch="convert" # in my case I need to watch convert
emailAddress="root@host" # this is my main emailaddress
triggerValue=90 # if the CPU use is above 90% send an email. DO NOT USE a DOT or COMMA!
tempFileName=tmp-cpu # some name of the temp file for the ps, grep data

ps auxww | grep "$processToWatch" | grep -v grep > /tmp/$tempFileName
export LINE
(
read LINE
while [ -n "$LINE" ]
do
set $LINE
read LINE
if [ $(echo "$3" | sed -e 's/\.[0-9]*//g') -gt $triggerValue ]; then
mail -s "CPU message alert for: $processToWatch" $emailAddress <<-END
This is to inform you that the following process: $processToWatch with PID (Process ID) $2 is now using more than your preset $triggerValue value.

Process: $processToWatch is using: $3 of CPU power!
The command used is: $11
END
fi
done
)< /tmp/$tempFileName

falko
13th March 2006, 18:56
Looks good! :)

edge
13th March 2006, 21:04
Looks good! :)

Thanks falko..
For sure the code can be made better, but for now it will do.
(I started coding in bash 2 days ago)

Bash looks like a fun language to use.. I'm going to read some more things about it.

falko
14th March 2006, 00:31
This is a good starting point: http://www.tldp.org/LDP/abs/html/index.html