开发者

Run Ruby script in the background

开发者 https://www.devze.com 2023-03-13 15:12 出处:网络
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.

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?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消