Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I work on large-scale datasets. When testing new software, a script will sometimes sneak up on me, quickly grab all available RAM, and render my desktop unusable. I'd like a way to set a RAM limit for a process so that if it exceeds that amount, it will be killed automatically. A language-specific solution probably won't work, as I use all sorts of different tools (R, Perl, Python, Bash, etc).

So is there some sort of process-monitor that will let me set a threshold amount of RAM and automatically kill a process if it uses more?

share|improve this question
    
1  
Last time i asked this (around a week ago) all the answers i got where "it's a bad idea, you shouldn't do it". Good Luck getting an answer though :) –  Chriskin Jul 27 '11 at 17:09
    
@ Uri : At that thread i only got a partial (only for the cpu part of my question) - and not optimal - answer. He asks about RAM usage. –  Chriskin Jul 27 '11 at 17:11
    
Uri, there's a complete lack of useful ideas in that thread. Posting a link to it isn't particularly helpful. –  chrisamiller Jul 27 '11 at 17:12
1  
Hrmm - this superuser thread seems to have a reasonable suggestion using ulimit: superuser.com/questions/66383/restrict-ram-for-user-or-process –  chrisamiller Jul 27 '11 at 17:19
add comment

2 Answers 2

I would strongly advise not to do it. As suggested by @chrisamiller , setting ulimit will limit the RAM available with process.

But still if you are insisting then follow this procedure.

  1. Save the following script as killif.sh:

    #!/bin/sh
    
    if [ $# -ne 2 ];
    then echo "Invalid number of arguments"
    exit 0
    fi
    
    while [ 1=1 ];
    do
    
    SIZE=`pmap $1|grep total|cut -d" " -f13`
    SIZE=${SIZE%%K*}
    SIZEMB=$(($SIZE/1024))
    echo "Process id ="$1" Size = "$SIZEMB" MB"
    if [ $SIZEMB > $2 ];
    then echo "SIZE has exceeded.\nKilling the process......"
    `kill -9 $1`
    echo "Killed the process"
    exit 0
    else
    echo "SIZE has not yet exceeding"
    fi
    
    sleep 10
    done
    
  2. Now make it executable.

    chmod +x killif.sh

  3. Now run this script on terminal. Replace PROCID with actual process id and SIZE with size in Mb.

    ./killif.sh PROCID SIZE

    For example: ./killif.sh 132451 100

If SIZE is 100 then process will be killed if it's ram usage goes up beyond 100 MB.

Caution: You know what are you trying to do. Killing process is not a good idea. If that process has any shutdown or stop command then edit the script and replace kill -9 command with that shutdown command.

share|improve this answer
    
I fixed the formatting of the code for you. –  Nathan Osman Jul 27 '11 at 19:08
    
i would consider putting little sleep there, for example sleep 0.5... –  Denwerko Jul 27 '11 at 19:32
    
@George Edison How did you do that. When I tried to paste, it was giving me weirdly formatted text. Denwerko: Where would you like to put the sleep. I have already put a sleep of 10 seconds in the while loop –  Amey Jah Jul 28 '11 at 15:21
    
Thanks for the solution, as noted below, I may check it out. Why all the dire warnings about killing processes, though? Cluster management software (LSF, PBS, et al) does this all the time when a process exceeds the amount of resources that it requested, with no dire consequences. I agree that this is not a good solution for new users who are likely to misapply it, but in the right circumstances, this can be quite useful. –  chrisamiller Jul 28 '11 at 18:49
    
@chrisamiller I have put up a warning for future readers. In your case, you know the implications but future reader of this post might not be aware of such implications. –  Amey Jah Jul 28 '11 at 19:46
show 1 more comment

I hate to be the guy who answers his own question, but this morning I found an alternative method, wrapped into a nice little utility. It'll limit CPU time or memory consumption:

https://github.com/pshved/timeout

I'm giving this one a shot first but upvotes to Amey Jah for the nice answer. I'll check it out if this one fails me.

share|improve this answer
    
+1 Answering your own question is OK! –  Tom Brossman Sep 25 '12 at 11:12
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.