How hard is it to create some kind of timeout procedure for using to much memory or cpu on a linux/unix server? What would you have to do to do this?

This happens WAY to often. I understand you may need full resources if you are doing something with heavy resources. But when you leave something running for over 3 days that is ABSOLUTELY ridiculous. If you can't get what your doing done in like an hour then you should be on a personal server. Hundreds of people need to use that server and as you can tell from the screenshot it is an old server with limited resources. Can you think of a way to stop this after like an hour? In this case the person was using 99.9% of the server.

enter image description here

  • Just to clarify, are you looking for a way to see what processes are consuming over X amount of CPU/MEM etc ? – user14755 Oct 12 '15 at 2:49
  • Can you please explain further? Perhaps provide an example of what you want? – Ryan Babchishin Oct 12 '15 at 3:43
  • @RyanBabchishin I added more details. – rockstar797 Oct 12 '15 at 4:41
  • @DarkHeart I added more details. – rockstar797 Oct 12 '15 at 4:41
  • I don't know about software, but you could write a script. If you can't, I can make a simple one and post it as an answer for you. – Ryan Babchishin Oct 12 '15 at 5:01

How's this? It may need some tweaking, but I'm sure you can figure that out with this as a starting point. You can run it from cron. If for some reason it doesn't work, it might be a formatting error due to the web site.


# Internal field separator                                                                                                                                                                                

# Settings
minUid=1000 # Minimum UID to kill (protect services and root)
maxCpu=25 # Maximum %CPU a process can consume for $maxTime
maxTime=3600 # Maxumum a time a process can run, in seconds

# Load and format process list
psList=$(ps -eo pid,uid,pcpu,etimes,args | awk '{ print $1" "$2" "$3" "$4" "$5}' | tail -n+2)

echo Please wait...

# Do this for each process
for process in $psList ; do {
        pid=$(echo $process | cut -d' ' -f1) #pid
        uid=$(echo $process | cut -d' ' -f2) #uid
        cpu=$(echo $process | cut -d' ' -f3) #cpu usage
        cpu=$(printf "%.0f" $cpu) #convert float to integer
        etimes=$(echo $process | cut -d' ' -f4) #elapsed time since pid was started
        cmd=$(echo $process | cut -d' ' -f5) #command name

        # Check if the UID is ok to kill
        if [ $pid -ge $minUid ]; then {

                # Check if it matches time + cpu usage restrictions
                if [ $etimes -ge $maxTime ] && [ $cpu -ge $maxCpu ] ; then {
                        # Indentified process to kill
                        echo "kill pid=$pid uid=$uid cpu=$cpu etimes=$etimes cmd=$cmd" 
                        kill -KILL $pid
                } fi
        } fi

} done
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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