Skip to content

Instantly share code, notes, and snippets.

@ramontayag
Created November 3, 2011 10:01
Show Gist options
  • Save ramontayag/1336175 to your computer and use it in GitHub Desktop.
Save ramontayag/1336175 to your computer and use it in GitHub Desktop.
Kill stuck imagemagick convert processes
#!/usr/bin/env ruby
LIMIT=30 #limit on seconds
PROCESS="convert" #change to process u want to grep
COMMAND="ps -eo pid,cmd,etime"
process_list = `#{COMMAND} | grep #{PROCESS}`
process_list = process_list.split("\n") # make an array of process strings
process_list = process_list.select {|process| process !~ /grep/} # remove the grep process
process_list.each do |process|
process_id = `echo #{process} | awk {'print $1'}`.to_i
process_command = `echo #{process} | awk {'print $2'}`
process_time = `echo #{process} | awk {'print $3'}`
process_time_split = process_time.split(':')
process_time_minutes, process_time_seconds = process_time_split[0].to_i, process_time_split[1].to_i
process_started_ago_in_seconds = process_time_minutes * 60 + process_time_seconds
if process_started_ago_in_seconds > LIMIT
Process.kill 9, process_id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment