2

I have a Ruby script that runs/query a database and enters/deletes information every 30 seconds. I run it from command line using:

ruby worker.rb

I am sshing into the server. When I close the terminal the worker stops running. Is there a way to ensure it keeps running?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
0xSina
  • 20,973
  • 34
  • 136
  • 253
  • possible duplicate of [Make a Ruby program a daemon?](http://stackoverflow.com/questions/3688550/make-a-ruby-program-a-daemon) – tadman Nov 21 '12 at 15:36

2 Answers2

6

Yes. The simple way is to use nohup and &:

nohup ruby worker.rb &

Will start it, return control to the shell, and let it run on its own independent of the terminal. Another option is to make it a daemon. This code goes after setup code:

Process.daemon
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Linuxios
  • 34,849
  • 13
  • 91
  • 116
2

For best control you would like to use Screen or Tmux

You will be able to start the script as usual(without daemon-ing) and see the output.

You'll can close your SSH session and script(s) will continue running.

Next time you login, you get same environment, just like you never logged out.