Quick Review: Boxee Box
December 27, 2011 – 12:22 am | 3 Comments

Some of the technical issues with Boxee Box could have been fixed if the dev team was paying more attention to addressing the bugs rather than adding “features” of dubious value. In the final analysis, for the price and ease of use, Boxee Box is the best in its class and price range. You just need to be mindful of its limitations and buy it in hope of future improvements to its usability.

Read the full story »
Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Scripts

Controlling Process CPU Utilization

Submitted by on March 9, 2009 – 12:36 pmNo Comment
Controlling Process CPU Utilization

Let’s say there is a process on your Unix/Linux system that sometimes tends to consume all CPU resources and become unresponsive. At the same time, you do not want to terminate the process at the first sign of trouble, because momentary high CPU utilization may be legitimate. The solution is to continuously calculate the running average of CPU utilization. Korn shell array is a good tool for storing intermediate values and calculating the average. Below is a sample script that will terminate the monitored process (process.bin) if it exceeds CPU utilization limit during the specified period of time.

#!/bin/ksh
 
configure() {
        COMMAND="process.bin"              # Name of the process being monitored
        CPULIMIT=99                             # CPU threshold
        LOOP=10                                   # Monitor process every so many seconds
        INTERVAL_DURATION=1                # Take CPU utilization readings every so many seconds
        INTERVAL_COUNT="0 1 2 3 4"         # Calculate average based on this many readings
}
 
pid() {
        PID=99999999
        PID=$(ps -ef | grep $COMMAND | grep -v grep | awk '{print $2}' | sort | uniq | tail -1)
}
 
cpu() {
        CPU=0
        CPUAVG=0
        CPU=$(top -b -n 1 -p $PID | grep $COMMAND | awk '{print $9}')
 
        if [ $CPU -ge $CPULIMIT ]
        then
                for i in $INTERVAL_COUNT        # If CPU load exceeds $CPULIMIT, determine average CPU load
                do
                        array[$i]=$(top -b -n 1 -p $PID | grep $COMMAND | awk '{print $9}')
                        sleep $INTERVAL_DURATION
                done
 
                CPUAVG=$(echo "scale = 0 ; (${array[0]}+${array[1]}+${array[2]}+${array[3]}+${array[4]})/5" | bc -l)
        fi
}
 
terminate() {
        if [ $CPUAVG -ge $CPULIMIT ]
        then
                kill -9 $PID
                echo "Killed $PID at `date`"
        fi
}
 
# -------------------------------
# RUNTIME
# -------------------------------
configure               # Configure script parameters
 
i=1
while [ $i -eq 1 ]      # Run script in a loop every $LOOP seconds
do
        pid             # Aquire unique PID of the $COMMAND
        cpu             # Determine current CPU load for the $COMMAND
        terminate       # Kill $COMMAND if $CPULIMIT is exceeded
        sleep $LOOP
done

Popularity: 4% [?]

Related posts:

  1. Monitoring process CPU and memory usage
  2. Korn Shell Arrays

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.