Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script checking cpu usage of specific process

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.

like image 297
user2073780 Avatar asked Feb 14 '13 22:02

user2073780


People also ask

How do I monitor CPU usage on a specific process?

Right-click on the graph and select "Add Counters". In the "Available counters" list, open the "Process" section by clicking on the down arrow next to it. Select "% Processor Time" (and any other counter you want). In the "Instances of selected object" list, select the process you want to track.

How check CPU utilization for particular process in Linux?

Running the mpstat command on a Linux system will display an output like the one shown in figure 2. This command shows various CPU statistics including idle time, io wait time and steal time. Similar to the top command, the idle time shown here can be used to compute the CPU utilization using the same formula.

How do I monitor a single process in Linux?

Usually, we can use the Linux built-in top command. This command displays a real-time view of a running system in the command prompt. If we want to have an idea of a single process, we can use the -p parameter.


2 Answers

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}'
like image 131
Anton Kovalenko Avatar answered Sep 28 '22 15:09

Anton Kovalenko


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.

like image 40
that other guy Avatar answered Sep 28 '22 17:09

that other guy