Skip to content

Instantly share code, notes, and snippets.

@kzk
Created December 21, 2010 04:30
Show Gist options
  • Save kzk/749492 to your computer and use it in GitHub Desktop.
Save kzk/749492 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# 2010/12/21 Kazuki Ohta <[email protected]>
# Respawn unicorn child processes.
#
rails_root = ENV["RAILS_HOME"]
# master
unicorn_master_pid_file = File.expand_path("tmp/pids/unicorn.pid", rails_root)
unicorn_master_pid = File.read(unicorn_master_pid_file).to_i
# children
ps_output = `ps w --ppid #{unicorn_master_pid}`
ps_output_lines = ps_output.split("\n")
children = []
ps_output_lines.each do |line|
chunks = line.strip.split(/\s+/, 5)
pid, pcmd = chunks[0], chunks[4]
next if pid !~ /\A\d+\z/ or pcmd !~ /worker/
children << pid.to_i
end
children.each { |pid|
Process.kill :QUIT, pid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment