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.

  • 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, 2010 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). May 3, 2010 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, 2010 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, 2014 at 23:05

4 Answers 4


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.


I have created a script, kill-process, that kills some processes listed in an array, if CPU usage is greater than XX% for YY seconds or kill processes that's running more than ZZ seconds.

  • You can set XX, YY, ZZ in the top of the file.
  • You can use a ps or top for check processes.
  • There's a dry run mode too, to check but not kill.
  • In the end, the script sends an email if some processes were killed.

NOTE: Here is my repo on Github: https://github.com/padosoft/kill-process

Essential part of script (a code abstract for top command):

#!/usr/bin/env bash

#max cpu % load
#max execution time for CPU percentage > MAX_CPU (in seconds 7200s=2h)
#sort by cpu

#define a processes command name to check
declare -a KILLLIST
KILLLIST=("/usr/sbin/apache2" "/usr/bin/php5-cgi")

#iterate for each process to check in list

    #retrive pid with top command order by SORTBY
    PID=$(top -bcSH -n 1 | grep $PROCESS_TOCHECK | sort -k $SORTBY -r | head -n 1 | awk '{print $1}')

    CPU=$(top -p $PID -bcSH -n 1 | grep $PROCESS_TOCHECK | sort -k $SORTBY -r | head -n 1 | awk '{print $9}')
    TIME_STR=$(top -p $PID -bcSH -n 1 | grep $PROCESS_TOCHECK | sort -k $SORTBY -r | head -n 1 | awk '{print $11}')

    # Decode the top CPU time format [dd-]hh:mm.ss.
    IFS="-:" read c1 c2 c3 c4 <<< "$TIME_STR"

    #with top command time format is hh:mm.ss, so truncare seconds in c2

    if [ -n "$c4" ]
    elif [ -n "$c3" ]
      if [ "$CMD" = "ps" ]; then
      if [ "$CMD" = "ps" ]; then

    #check if need to kill process
    if [ $CPU -gt $MAX_CPU ] && [ $TIME_SEC -gt $MAX_SEC ]; then
        kill -15 $PID

bash killprocess.sh [dry|kill|--help] [top|ps] [cpu|time]
  • @DavidPostill I add an abstract of script code. Thank's. Jun 7, 2016 at 17:01
  • Much better ... :)
    – DavidPostill
    Jun 7, 2016 at 17:01

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:


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");
                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
        echo '' > ./auto_killer_data

    #We wait fo a while and repeate process
    sleep $Check_Repeating_Time\s;

I have faced the similar issue before, and here is the small piece of python code to solve it:

Link to github repository

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .