Run Ruby script in background

I have a Ruby script that I need to constantly work in my Linux box. I tried nohup ruby ruby.rb& , but it doesn't seem to work.

How can I run a script in the background?

+7
source share
2 answers

See screen , which is a command line utility. Start with

 screen 

You will get a new shell that will be detached. Start your script with

 ruby whatever.rb 

And look how it works. Then press Ctrl - A Ctrl - D , and you will return to the original shell. You can leave the ssh session now, and the script will continue to work. At a later time, enter your block and enter

 screen -r 

and you must return to a separate shell.

If you use the screen more than once, you will need to select a screen session using pid, which is not so convenient. To simplify, you can do

 screen -S worker 

to start a session and

 screen -r worker 

to renew it.

+24
source

Depending on your needs:

 fork do Process.setsid sleep 5 puts "In daemon" end puts "In control script" 

In real life, you will have to open STDOUT / STDERR again.

+3
source

All Articles