Heres a little trick to kill of unwanted processes that are running for more than X minutes on Linux.
I use this on a production server, where imagemagick and ffmpeg are executed many thousands of times per day and in normal conditions each process takes 0.x to 30 seconds. On rare cases they seem to run out of control and never finish execution or take longer than seems reasonable and worthwhile for the particular task. To free up resources taken by such processes, I decided it would be ok to just kill them if they are running for more than 10 minutes. It may not be a very safe and beautiful way to deal with such processes, but it’s working well for me for years now, so I’ll share…
After searching around and trying some obscure scripts I found out all I have to do for my needs is to periodically run killall with the –older-than argument. So, to kill ffmpeg that’s running for more than 10 minutes, run this command:
root killall -9 --older-than 10m ffmpeg
If you want it to kill off long running processes automatically, add this to the /etc/crontab
#kill ffmpeg processes */2 * * * * root killall -9 --older-than 10m ffmpeg >/dev/null 2>&1 #kill imagemagick (convert) processes */2 * * * * root killall -9 --older-than 10m convert >/dev/null 2>&1
Warning: keep in mind, in this manner any instance of the program run by anyone will be killed. This may cause confusion and errors in the future, so only do this if you know what it does and have reasons to do so.
You may want to kill only one users processes, if the machine may ever be used by someone to run the same program for huge files or there are other valid reasons for it to run longer. If you want to kill only one users processes, put it in this users cron or use his username instead of root.