9

I have a Ruby script that I need to have running all the time in my Linux box. I tried nohup ruby ruby.rb& but it seems it doesn't work.

How can I have the script running in background?

6
  • No. But when I do "nohup" I imagine I should see the result in the output file and I don't. I just want to make sure it is running.
    – donald
    Jun 17, 2011 at 20:09
  • Well when you originally run the command with the & do you get a PID?
    – cmwright
    Jun 17, 2011 at 20:10
  • Yes, ~# nohup ruby unread_mail.rb & [4] 15859
    – donald
    Jun 17, 2011 at 20:11
  • okay, so you can run ps -a and verify whether the process is running. The only reason it wouldn't be is that the script itself is terminating
    – cmwright
    Jun 17, 2011 at 20:14
  • Strange, I have same issue. Why does Ruby not output while node.js script does? I use nohup with output redirection same way for both. Ruby runs fine without nohup.
    – David
    Oct 21, 2015 at 20:53

2 Answers 2

29

Have a look at screen which is a command-line utility. Start it with

screen

You will get a new shell which is detached. Start your script there with

ruby whatever.rb

And watch it run. Then hit Ctrl-A Ctrl-D and you should be back at your original shell. You can leave the ssh session now, and the script will continue running. At a later time, login to your box and type

screen -r

and you should be back to the detached shell.

If you use screen more than once, you will have to select the screen session by pid which is not so comfortable. To simplify, you can do

screen -S worker

to start the session and

screen -r worker

to resume it.

4

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 reopen STDOUT/STDERR.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.