Ruby脚本作为服务 [英] Ruby script as service
问题描述
好吧,标题说的一切。我有一个ruby脚本我想运行作为一个服务(一个我可以启动和停止)在我的Linux盒子。我可以在Windows 此处找到如何执行此操作。
Well, the title say it all. I have a ruby script I want running as a service (one I can start and stop) on my Linux box. I was able to find how to do it on Windows here
有些读数指向创建守护程序或cron任务。
Some readings point to creating daemons or cron tasks.
我只需要一些简单的东西,盒的重新启动,并可以停止/开始每当我。我的脚本有一个内部睡眠调用,并运行在永恒循环
I just need something simple I can call on my box's reboot, and can stop/start whenever I please. my script has an internal sleep call, and runs in "eternal loop"
提前感谢
推荐答案
我实际上找到了一个更好的方式,通过使用ruby脚本。
I've actually found a much better way of doing that by using ruby scripts.
这是我做的:
首先,我安装了守护程序
First of all, I installed daemon
gem install daemons
然后我:
require 'rubygems'
require 'daemons'
pwd = File.dirname(File.expand_path(__FILE__))
file = pwd + '/runner.rb'
Daemons.run_proc(
'my_project', # name of daemon
:log_output => true
) do
exec "ruby #{file}"
end
然后我创建一个名为runner.rb的文件,调用我的脚本如:
I then create a file called runner.rb, in which I can call my scripts such as:
require "/var/www/rails/my_project/config/environment"
Post.send('details....')
Daemons是一个伟大的宝石!
Daemons is a great gem!
这篇关于Ruby脚本作为服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!