-1

I'm sure there is a simple way to do this but I can't seem to find it.

I simply want my ruby program to run all the time, so if it exits or terminates for some reason I want it to automatically re-run.

Any ideas of how to achieve this?

-Thanks

m.ipp
  • 19
  • 6
  • I suppose what I mean is, if my program crashes or exits or something, is there a method I can call using at_exit or something similar to jump back and run the program again? Sort of a failsafe. Is that what a daemon does? – m.ipp Jul 13 '16 at 17:07
  • No this isn't really possible, have a look at [this](http://stackoverflow.com/questions/696839/how-do-i-write-a-bash-script-to-restart-a-process-if-it-dies) instead. – Nabeel Jul 13 '16 at 17:18
  • I think I got it. In my main code block called 'main', I told at_exit to call 'main_reboot' which has a break and then re-calls 'main'. I don't know if that's a best practice but it seems to be working – m.ipp Jul 13 '16 at 17:26
  • Daemon processes can be built to always restart should it no longer be running. Should you wish to add anything outside of, "restart this script", you will need to engineer a solution which involves more persistent state storage (flat file, SQLite, etc) so that you can recover the application state. But at the bare minimum, daemons get you halfway there. – Makoto Jul 13 '16 at 18:03
  • Restarting a daemon is, at least in the *nix world, best done by something else. monit, as an example of one of many such tools, will detect a daemon that has stopped running and restart it. – Wayne Conrad Jul 13 '16 at 22:07
  • If there is a hardware or operating system issue you obviously cannot include any code to deal with that. If you you want to restart your program if something unexpected happens within the code, there's a problem with the code that needs to be fixed. Having some sort of restart mechanism only masks the problem. Am I missing something? – Cary Swoveland Jul 14 '16 at 02:16

2 Answers2

0

While you can write this sort of thing yourself, it's probably better to start with a library like daemons and use that to manage your process.

As shown in the README it's pretty simple:

require 'daemons'

Daemons.run('service.rb')

Where service.rb is the entry point into your main application that you want to keep running.

Your approach of force-restarting the application within the same Ruby process might seem to work, but it's liable to cause chaos later on. What if your program has entered a state where doing anything leads to an exception, and that causes a restart, which immediately triggers a new exception? What if it crashed because the internal state is mangled?

Let it die, restart it cleanly with a supervisor process like Daemons, and you'll be much better off. These supervisors often throttle restarts to avoid pinning your server with thousands of reboots per second if it's having trouble.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

The restart logic is properly done for you by the monitor you use in your server (systemd or monit can do that pretty easily but there are many others).

If you are doing a service you don't want it to restart every time because the it will be too costly, the process is held in a event loop that waits for income requests but this is usually done for you in the library you use to handle the protocol, so you write only the handler for the incoming request.

Jonas Fagundes
  • 1,519
  • 1
  • 11
  • 18