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

Discussion in 'Programming/Scripts' started by edge, Mar 11, 2006.

  1. edge

    edge HowtoForge Supporter

    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.

    (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
     
  2. falko

    falko Super Moderator

    You could parse the output of
    Code:
    ps aux
     
  3. edge

    edge HowtoForge Supporter

    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.

    Code:
    ********* 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?
     
  4. falko

    falko Super Moderator

    Why don't you use something like
    Code:
    ps aux|grep coldfusion|grep -v grep
    to find all processes related to coldfusion?
     
  5. edge

    edge HowtoForge Supporter

    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

    Code:
    #!/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
    
     
    Last edited: Mar 12, 2006
  6. edge

    edge HowtoForge Supporter

    Okay.. I got it :)

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

    Code:
    #!/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
    
     
    Last edited: Mar 13, 2006
  7. falko

    falko Super Moderator

    Looks good! :)
     
  8. edge

    edge HowtoForge Supporter

    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.
     
  9. falko

    falko Super Moderator

Share This Page