Watching ImageMagick and killing when they run too long

imagemagickmonitoring

I use a program that calls ImageMagick's command-line commands. These are supposed to run instantly — perhaps 1 or 2 seconds. However, lately I've noticed a bunch of them got stuck.

Frozen processes

I use monit to monitor the server and watch certain things when the memory goes overboard. What would be a good way to watch ImageMagick? I'd like to kill the processes if they've been alive for 1 minute. However, I don't want to do a blanket kill all convert processes.

What do I do?

Thanks!

Best Answer

You can parse the output of ps -ef (or ps -axf on BSDish systems) to find processes with a start time (STIME) greater than X, or you can use TIME (CPU seconds used).


Implementation of this solution is left as an exercise for the reader, it's relatively simple. Hints:

  • You can do it with a shell script (for line in top; do ... done)
  • grep and awk (or cut) are your friends
  • STIME changes format depending on how long the process has been running.
    If we're talking within a 24 hour period you shouldn't have to worry about that, and you can kill any process that doesn't match the expected format for something started <24 hours ago.
  • Test your script first (echo "I want to kill $PID ($line)") before you let it really shoot processes in the head!
Related Topic