Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I don't know about your computer but when mine is working properly no process is sucking 95%+ over time. I would like to have some failsafe that kills any processes behaving like that. This comes to mind because when I woke up this morning my laptop had been crunching all night long on a stray chromium child process.

This can probably be done as a cron job, but before I make it a full time job creating something like this I'd thought I should check here. :) I hate reinventing the wheel.

share|improve this question
2  
This is for sure possible (using ps and similar tools). But the real question is, do you really want that? Check-Programs of all kind can grind pretty high cpu usage for quiet some time, also image-processing can take that, too. You should ask yourself a question: Is it worth the aftermath if it kills the wrong process? –  Bobby May 3 '10 at 13:16
3  
You almost always should fix the underlying problem. It's not really a good idea to blindly kill processes based on their resource usage. That's what the kernel's OOM-killer is for (at least for memory). –  Dennis Williamson May 3 '10 at 13:32
    
I'd also have to argue against doing it with any kind of auto-kill script/job. Maybe auto-notify is the right way and you can decide to kill or not. Fix underlying problem once it's identified. If your computer can send email to your phone then you can do this "real-time" or close enough. –  hotei Aug 23 '10 at 16:45
    
many makefile compile source code with make $(nproc), this will take up to approx. 100% cpu usage most of the time, and it should not be killed. I would recommend you to investigate the root cause and see what takes up so much resources, then find a way to stop/remove it. –  boh May 22 at 23:05
add comment

2 Answers

Others have encountered this problem, and while there doesn't seem to be any prevention mechanisms builtin to Ubuntu, there are some ideas about how to improve this.

There's a similar Serverfault question that mentions Monit might be able to help.

share|improve this answer
add comment

Some time ago I made a script for my University classes, that was controlling system resources (CPU and/or RAM) usage. You can easily changed maximum prohibited usage by editing variables at the script start. Script should be started in backgroung like that:

nohup ./auto_killer.sh &

Script is looking for processes that use too much resources every N seconds. If some process use too much of resources, the user that runs it get warning message on his console. If same process is spoted 2 times in row, it gets killed (And user gets info about it).

Here is the code:

#!/bin/bash

Check_Repeating_Time=3; # in seconds
Max_CPU_Usage='25.0'; #%
Max_RAM_Usage='2.0'; #%
Log_Path='/var/log/auto_killer_log'; # path to file when killing logs will be writed

while [ 1 ]; do

    ps -aux | 
    awk '{
        Username = $1;
        Proc_Name = $11;
        CPU_Usage = $3;
        RAM_Usage = $4;
        PID = $2;
        TTY = $7;

        if((CPU_Usage >= '$Max_CPU_Usage' || RAM_Usage >= '$Max_RAM_Usage' ) &&  !($1 == "USER" || $1 == "root" || $1 == "daemon" || $1 == "mysql" || $1 == "avahi" || $1 == "polkitd"))
        {
            Func_Num_of_Ocur = "cat ./auto_killer_data | grep "PID" | wc -l";
            Func_Num_of_Ocur |getline Str_Num_Of_Ocur;              

            if(Str_Num_Of_Ocur == "0")
            {
                system ("echo \"\" >> /dev/" TTY);
                system ("echo \"Process "Proc_Name" used to much of resources. It will be killed in '$Check_Repeating_Time' seconds if it wont stop!\" >> /dev/" TTY );
                system ("echo \"\" >> /dev/" TTY);
                system ("echo "PID" >> ./auto_killer_data.new");
            }
            else
            {
                system ("echo \"\" >> /dev/" TTY);
                system ("echo \"Process "Proc_Name" was killed because it used to much of system resources!\" >> /dev/" TTY );
                system ("echo \"\" >> /dev/" TTY);
                system ("kill -9 " PID);
                Data = "date";
                Data |getline Str_Data;
                system ("echo \""Str_Data"  "Username"  "Proc_Name" "TTY"\" >> '$Log_Path'");
            }
        }
    }';

    if [ -e ./auto_killer_data.new ]; then
        mv ./auto_killer_data.new ./auto_killer_data
    else    
        echo '' > ./auto_killer_data
    fi

    #We wait fo a while and repeate process
    sleep $Check_Repeating_Time\s;
done;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.