(Ruby) Write service with daemons gem

How to make a ruby script run only one instance at a time? and with start/stop command?

Actually, it's very simple, just use Daemons gem.

installation (ubuntu):
sudo gem install daemons

Simplest example: test.rb
require 'rubygems'
require 'daemons'

Daemons.run_proc('myproc.rb') do
loop do
sleep(5)
puts "Hello World"
end
end

Than you can use following commands:
ruby test.rb [start|stop|restart|run|zap]

(Run 'ruby test.rb' will get command usage.)

And when you try to run 'test.rb start' twice. you will get:
ERROR: there is already one or more instance(s) of the program running



==Traditional Chinese version==


如何讓一支ruby程式一次只有一個instance?並且有start/stop等指令呢?

其實,這非常簡單,只要用Daemons gem就可以了。

安裝 (ubuntu):
sudo gem install daemons

最簡單的範例: test.rb
require 'rubygems'
require 'daemons'

Daemons.run_proc('myproc.rb') do
loop do
sleep(5)
puts "Hello World"
end
end

然後你就可以用以下指令了:
ruby test.rb [start|stop|restart|run|zap]

(執行 'ruby test.rb' 會提示你指令用法)

當你嘗試執行 'test.rb start' 兩次,你會得到以下錯誤:
ERROR: there is already one or more instance(s) of the program running

留言

這個網誌中的熱門文章

神之雫 的 雫 (雨下) 怎麼唸、意思是什麼

[已解決] 入肉 (肏) 的注音/怎麼唸?意思是什麼?(兒童不宜)

哆啦A夢放上任何東西都無違和感、彷彿有音效的滑鼠墊 這裡買得到