(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
喜歡這篇文章嗎?你可能也會喜歡我的學習電子報,我會分享經過篩選的優質內容 + 學習心得
📚 訂閱我的學習電子報
📖 涵蓋主題:
🧑💻 軟體工程
🎯 生產力
💰 商業理財
🧳 創業
🐱 奇怪的知識增加了
💭 思考人生心得
🎁 現在訂閱就送我個人非常愛用「學習任何領域皮毛的 AI Prompt」!
立即免費訂閱 →
✨ 不浪費你的時間,只分享達到我標準的優質內容