run Ruby/EventMachine script as a system service
-
13-10-2019 - |
Question
I've written a simple UDP server in Ruby using EventMachine. I'd like to keep it always running on my Linux box. Suggestions on how to wrap it up as a system service or in some other form that launches at start-up, stays running, can be monitored?
Solution
As you are on linux you can use daemons gem
OTHER TIPS
The Thin webserver, which is built on top of EventMachine, uses the daemons gem: https://github.com/macournoyer/thin/blob/master/lib/thin/daemonizing.rb
To keep it running, use Monit, which can be configured to check that the process is running, restart it if it's not, or restart if it starts using too much system resources, or an endless array of other possible conditions.
I would use Cron with [@restart][1]. A well behaved daemon should check if its already running before running again.
[1]: https://help.ubuntu.com/community/CronHowto#Advanced Crontab
All these answers are outdated. Ruby has perfect Process.daemon
method: http://www.ruby-doc.org/core-2.1.0/Process.html#method-c-daemon
Just add Process.daemon
in your application before EM.run
and all should work.