1

Right now, after having loaded everything my executable runs my app like this:

Rack::Handler::pick(['puma']).run VCK::App

and it works, but it blocks the terminal (which is ok for development)

How do I get this to start as a daemon programmatically from within my executable?

EDIT:

Code I use to start sinatra as a daemon:

    if options[:daemonize]
        pid = fork {Rack::Handler::pick(['puma']).run VCK::App}
        File.open(pid_file_loc, 'w') {|f| f.write(pid)}
        Process.detach(pid)
    else
        Rack::Handler::pick(['puma']).run VCK::App
    end

Code I use to stop Sinatra Daemon:

Process.kill(15, File.read(pid_file_loc).to_i)
Thermatix
  • 2,757
  • 21
  • 51

1 Answers1

3

You can daemonize any ruby process from within your code by using Process#daemon

Anthony
  • 15,435
  • 4
  • 39
  • 69
  • 2
    This is not a daemon, it's just a process running in the background with its standard output/error going to the console. And once the shell is closed the sessions will receive a HUP signal and close the ruby process. At the very least one should do something like "nohup ruby server.rb > /var/tmp/server.out 2>&1 &" to prevent the process from closing on the hangup signal. – Steeve McCauley Mar 07 '16 at 14:30