rubydaemonruby-2.1ruby-daemons

Ruby Process.daemon: turning on/off


I am trying to daemonize a Ruby script, running on 2.1.1.

My daemon part of the code is like this:

case ARGV[0]
when "-start"
  puts "TweetSearcher started."
  Process.daemon
when "-stop"
  Process.kill(9,Process.pid) 
else
  puts "Lacks arguments. Use -start/-stop"
  abort
end

However, it looks like that the Process.kill(9,Process.pid) is not killing what I wanted to. I want to kill a previous "ruby tweetsearcher.rb -start", already running in background.

How do I proceed?


Solution

  • Typically, the PID is stored in a file that is then read to stop it.

    Calling Process.kill(9,Process.pid) kills the "stopper" process itself, rather than the one it's trying to stop.

    Here's a guide to writing daemons in Ruby: http://codeincomplete.com/posts/2014/9/15/ruby_daemons/

    As you can see, it's not a trivial process.

    Here is another blog that suggests that you should not try to daemonize at all, but instead rely on a process monitoring system to take care of those concerns: https://www.mikeperham.com/2014/09/22/dont-daemonize-your-daemons/