Run Ruby script in the background

后端 未结 2 1915
再見小時候
再見小時候 2021-01-31 23:30

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

相关标签:
2条回答
  • 2021-01-31 23:56

    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.

    0 讨论(0)
  • 2021-01-31 23:57

    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.

    0 讨论(0)
提交回复
热议问题