Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created October 16, 2016 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/83b98ac750bb8249c38acf0e32157c45 to your computer and use it in GitHub Desktop.
Save ahoward/83b98ac750bb8249c38acf0e32157c45 to your computer and use it in GitHub Desktop.
# when you really, really, really want to make sure your children get cleaned up you can generate a script
# that detaches and monitors *you*. if you disappear the assassin can ensure cleanup happens. even if
# you are 'kill -9d' or call 'exit!'
assassin = <<-__
pid = #{ Process.pid }
4242.times do
begin
Process.kill(0, pid)
rescue Object => e
if e.is_a?(Errno::ESRCH)
`#{ stop_server } >/dev/null 2>&1`
Process.kill(-15, pid) rescue nil
sleep(rand + rand)
Process.kill(-9, pid) rescue nil
end
exit
end
sleep(1 + rand)
end
__
IO.binwrite('/tmp/assassin.rb', assassin)
cmd = "nohup ruby /tmp/assassin.rb >/dev/null 2>&1 &" # need pure ruby / windoze compat
system cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment