Bash script checking cpu usage of specific process

19,511

Solution 1

The problem is that bash can't handle decimals. You can just multiply them by 100 and work with plain integers instead:

#!/bin/bash
declare -i app_pid
declare -i app_cpu
declare -i cpu_limit
app_name="top"
cpu_limit="5000"
app_pid=`ps aux | grep $app_name | grep -v grep | awk {'print $2'}`
app_cpu=`ps aux | grep $app_name | grep -v grep | awk {'print $3*100'}`
if [[ $app_cpu -gt $cpu_limit ]]; then
     echo "crap"
else
     echo "we're good"
fi

Keep in mind that CPU percentage is a suboptimal measurement of application health. If you have two processes running infinite loops on a single core system, no other application of the same priority will ever go over 33%, even if they're trashing around.

Solution 2

I recommend taking a look at the facilities of ps to avoid multiple horrible things you do.

On my system (ps from procps on linux, GNU awk) I would do this:

ps -C "$app-name" -o pid=,pcpu= | 
    awk --assign maxcpu="$cpu_limit" '$2>maxcpu {print "crappy pid",$1}'

Solution 3

#!/bin/sh
PROCESS="java"
PID=`pgrep $PROCESS | tail -n 1`
CPU=`top -b -p $PID -n 1 | tail -n 1 | awk '{print $9}'`
echo $CPU
Share:
19,511
user2073780
Author by

user2073780

Updated on June 13, 2022

Comments

  • user2073780
    user2073780 almost 2 years

    First off, I'm new to this. I have some experience with windows scripting and apple script but not much with bash. What I'm trying to do is grab the PID and %CPU of a specific process. then compare the %CPU against a set number, and if it's higher, kill the process. I feel like I'm close, but now I'm getting the following error:

    [[: 0.0: syntax error: invalid arithmetic operator (error token is ".0")

    what am I doing wrong? here's my code so far:

    #!/bin/bash
    declare -i app_pid
    declare -i app_cpu
    declare -i cpu_limit
    app_name="top"
    cpu_limit="50"
    app_pid=`ps aux | grep $app_name | grep -v grep | awk {'print $2'}`
    app_cpu=`ps aux | grep $app_name | grep -v grep | awk {'print $3'}`
    if [[ ! $app_cpu -gt $cpu_limit ]]; then
         echo "crap"
    else
         echo "we're good"
    fi
    

    Obviously I'm going to replace the echos in the if/then statement but it's acting as if the statement is true regardless of what the cpu load actually is (I tested this by changing the -gt to -lt and it still echoed "crap"

    Thank you for all the help. Oh, and this is on a OS X 10.7 if that is important.

  • user2073780
    user2073780 over 11 years
    Thanks for the help everyone! as for the %CPU, we actually have a process that starts eating CPU, get's to 100-200% and it then prevents the user from signing into the computer, killing the process causes it to restart and sit at .1% for a long time. I plan on having this run this as a Launch Daemon "on demand" so it should kill the process, so users don't have to reboot 1/3-1/2 the time the screen locks.
  • Sandeep C
    Sandeep C almost 6 years
    What if there are multiple processes with the same name and I just need to find the one which is above the set CPU?
  • Sandeep C
    Sandeep C over 5 years
    ./killJava.sh: line 7: 2941 8715: syntax error in expression (error token is "8715") ./killJava.sh: line 8: 10 60: syntax error in expression (error token is "60") What are these errors?
  • that other guy
    that other guy over 5 years
    @SandeepC It means there are multiple processes, which the original script did not account for
  • that other guy
    that other guy over 5 years
    To do something for each item in a list, use a loop