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


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


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


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 pm 5 Comments

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.


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=$(ps -ef | grep $COMMAND | grep -v grep | awk '{print $2}' | sort | uniq | tail -1)

cpu() {
        CPU=$(top -b -n 1 -p $PID | grep $COMMAND | awk '{print $9}')

        if [ $CPU -ge $CPULIMIT ]
                for i in $INTERVAL_COUNT        # If CPU load exceeds $CPULIMIT, determine average CPU load
                        array[$i]=$(top -b -n 1 -p $PID | grep $COMMAND | awk '{print $9}')
                        sleep $INTERVAL_DURATION

                CPUAVG=$(echo "scale = 0 ; (${array[0]}+${array[1]}+${array[2]}+${array[3]}+${array[4]})/5" | bc -l)

terminate() {
        if [ $CPUAVG -ge $CPULIMIT ]
                kill -9 $PID
                echo "Killed $PID at `date`"

# -------------------------------
# -------------------------------
configure               # Configure script parameters

while [ $i -eq 1 ]      # Run script in a loop every $LOOP seconds
        pid             # Aquire unique PID of the $COMMAND
        cpu             # Determine current CPU load for the $COMMAND
        terminate       # Kill $COMMAND if $CPULIMIT is exceeded
        sleep $LOOP
Print Friendly, PDF & Email


  • Balla says:

    I understand the concept of virtualization but I still don’t understand how it saves energy? How does it make the Xeon Server processors more efficient? Any help would be greatly appreciated.

  • Jesse says:

    Opinions again, why is it when i ask a question i get these extrememly solid answers and then when i ask ,?
    ok, what do u suggest i do instead? everybody is suddenly gone?

    Additional Details

    3 minutes ago
    Opinions again please part 2….my computer did not come with restore disks, it has a separate partition,Local
    drive (D:) where it stores the recov prog., but if this not be what to do…… computer is for home use, it has lots o mem and i use bout 10%, it says do not need defrag, i have run ccleaner, spybot, I have an anti-virus program, but it is running extrememly slow, and it kinda locks up, not kinda it LOCKS UP quite frequently. What else can I do? I am not a pro by far. Help Please

    1 minute ago
    Opinions again please…i have been advised to completely restore my computer, destructive recovery, about eve
    ry 6 months or so??? What do you think about this advice?

    1 second ago
    the second set of added details being the original quest.
    maybe i shouldn’t open with opinions, since every1 here has only facts. Advice is what I need. I am not here for approval or verification that I am right, I DO NOT HAVE THE ANSWER.

  • Rishabh Bajpai says:

    Something is using up a large chunk of my CPU. I can see that the CPU is running at 10-20% but performance is affected in a jittery way when playing games and other things. I can hear it busily doing something in the box, which makes me think the HDD is busy. However, in the processes tab of task manager only task manager is using any CPU (equally either 0 or 1 cycles). I’ve killed off things like sidebar trying to isolate the issue. As far as I can tell, windows is simply refusing to tell me which services it’s running to chew up my cpu (intel quad 6600).

    This doesn’t happen all the time, and for a while now Vista’s indexing tool has been the main culprit. It’s been set to only index a bunch of document formats, emails, pictures, things I basically recognise.

    So a couple of questions:
    1. Does anyone know what is causing the problem on that information?

    2. Is there a way of revealing hidden processes chewing up CPU so I can better identify the problem?

    3. Is it possible to isolate all of windows’ functions and frustrations to a single core and otherwise leave me in peace to run games or other programs without the irritation of some essential task.

    (PS. Antivirus is nod32 and it’s not scanning.)

  • Xbox360king says:

    my laptop these days freezes and it takes ages to openup applications what shud i do so that my laptop will function normally, i checked with various antivirus and anti spyware there is no traces of virus or spyware, plz help. Thanks

  • Brendan O says:

    my norton keeps saying CPU is high but when i go into computer to check it all says its very low so which one is right ?

Leave a Reply

%d bloggers like this: