LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Script to watch potentially misbehaving processes and kill them if needed

Posted 09-30-2016 at 10:09 AM by the dsc
Updated 10-02-2016 at 02:04 PM by the dsc (small improvements / bug fixes)
Tags cpu, hang, kill

A second adaptation of an adaptation of this script:
http://searchenterpriselinux.techtar...sses-with-Bash

The original adaptation was supposed to put any misbehaving process under cpulimit leashes, I'm not sure it's working properly. I've made a more radical implementation intended for programs that are more likely to misbehave and probably hang with high cpu, when killing them after a while seems to be a good idea.

I think it could perhaps be somewhat easily adapted to have some sort of config file to list different processes and limits, becoming somewhat similar to the "auto nice daemon" (and), but perhaps also more easily adaptable to specific requirements.

But right now you use it as "prockiller.sh process-name".

I think it won't work well with processes that spawn dozens of sub-processes, like google chrome/chromium. As always, this isn't anything intended for serious business or anything. I came up with it specifically for mpv, which was hanging with high-cpu load sometimes, to kill it if I was not around or didn't notice the fan speeding up like crazy.

Code:
#!/bin/bash

LIMIT=${2:-2400}

while true
do

if pidof $1 ; then

info=(`ps -C "$1" -o pcpu,pid | tail -1 `)
USAGE=${info[0]%%.*}
PID=${info[1]}

USAGE=$(echo $USAGE*100 | bc -l )
echo $info

 if (( USAGE > LIMIT-800 ))
 then
  USAGE1=$USAGE
  PNAME1=$PNAME
	echo $1 -- usage $USAGE \(pid $PID\)
  sleep=2
sleep $sleep

info2=(`ps -C "$1" -o pcpu,pid | tail -1 `)

USAGE2=${info2[0]%%.*}
USAGE2=$(echo $USAGE2*100 | bc -l )
PID2=${info2[1]}
echo $1 -- usage $USAGE  \(pid $PID2\) ... limit: $LIMIT

[ ! $PID = $PID2 ] && highload=0

  ((USAGE2 > LIMIT )) && [ $PID = $PID2 ] && highload=$((highload+1)) && echo "high cpu load for $highload times!"
  ((highload==5)) && if [[ ! "$*" == *"-dry-run"* ]] ; then kill -kill $PID ; fi && highload=0 && echo "=============== had to kill $1 ============== "
  ((USAGE2 < LIMIT )) && highload=$((highload-1)) ; ((highload<0)) && highload=0
  
  
else
sleep=10
 fi
sleep ${sleep}

else

echo no $1 process yet...
sleep 10

fi

done
Posted in Uncategorized
Views 919 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration