回答
使用Daemonize.rb
require 'daemons'
Daemons.daemonize
很簡單的例子:http://github.com/utkarsh2012/backitup/blob/master/backitup.rb
如何安裝守護寶石:
gem install daemons
這是一個module來守護您的代碼。這是一個包含現有腳本的offshoot。
本質上,它歸結爲這個(特拉維斯威頓的Daemonize.rb,上面的第一個環節,修改了某些程序我前寫的年齡):
private
# This method causes the current running process to become a daemon
# If closefd is true, all existing file descriptors are closed
def daemonize(pathStdErr, oldmode=0, closefd=false)
srand # Split rand streams between spawning and daemonized process
safefork and exit# Fork and exit from the parent
# Detach from the controlling terminal
unless sess_id = Process.setsid
raise 'Cannot detach from controlled terminal'
end
# Prevent the possibility of acquiring a controlling terminal
if oldmode.zero?
trap 'SIGHUP', 'IGNORE'
exit if pid = safefork
end
Dir.chdir "/" # Release old working directory
File.umask 0000 # Insure sensible umask
if closefd
# Make sure all file descriptors are closed
ObjectSpace.each_object(IO) do |io|
unless [STDIN, STDOUT, STDERR].include?(io)
io.close rescue nil
end
end
end
STDIN.reopen "/dev/null" # Free file descriptors and
STDOUT.reopen "/dev/null" # point them somewhere sensible
STDERR.reopen pathStdErr, "w" # STDOUT/STDERR should go to a logfile
return oldmode ? sess_id : 0 # Return value is mostly irrelevant
end
# Try to fork if at all possible retrying every 5 sec if the
# maximum process limit for the system has been reached
def safefork
tryagain = true
while tryagain
tryagain = false
begin
if pid = fork
return pid
end
rescue Errno::EWOULDBLOCK
sleep 5
tryagain = true
end
end
end
啊,谷歌來救援!退房
http://fitzgeraldsteele.wordpress.com/2009/05/04/launchd-example-start-web-server-at-boot-time/
其中一個有用的博客提供寫的launchd plist中推出了紅寶石的Web應用程序服務器的一個例子。
很高興您認爲這有幫助!我個人喜歡launchd ...對於一個它可以重新啓動你的過程,如果它意外死亡。 – fitzgeraldsteele 2010-10-25 21:13:31
它不適用於Windows。 – Olathe 2013-02-12 17:50:33
需要看到後臺程序導軌寶石爲Rails 3(基於rails_generator):
https://github.com/mirasrael/daemons-rails
可以生成守護存根這樣的:
rails generate daemon <name>
特點:
- individual con每個守護特倫腳本
- 耙:每守護守護命令
- Capistrano的友好
- 應用範圍的控制腳本
- 監測API
- 可能多個後臺程序設置
- 1. Ruby守護進程Gem
- 2. Ruby守護進程vs守護進程gem:有哪些優點和缺點?
- 3. Ruby守護進程和頻率
- 4. 乾淨關機的Ruby守護進程
- 5. 動態Ruby守護進程管理
- 6. Ruby守護進程不會啓動
- 7. 使用python,守護進程
- 8. Linux守護進程
- 9. Python守護進程
- 10. iphone守護進程
- 11. MAC更改程序或守護進程?
- 12. java守護進程線程
- 13. Linux守護進程停止啓動 - 停止守護進程
- 14. init進程是一個守護進程
- 15. 使java.util.Timer成爲守護進程的正確方法
- 16. Java中的守護進程和非守護線程優先級
- 17. 可可的守護進程
- 18. PHP守護進程腳本
- 19. 停止Erlang守護進程
- 20. 用Clojure/JVM守護進程
- 21. git守護進程與LFS?
- 22. Authlogic&delayed_job&delayed_jobl_mailer&守護進程?
- 23. emacs守護進程和magit
- 24. Web.py守護進程通信
- 25. FreeBSD的守護進程庫
- 26. 守護進程的Makefile
- 27. Node.js WebSocket守護進程
- 28. 安裝守護進程
- 29. Linux守護進程啓動
- 30. Volttron init守護進程
這可能是一個愚蠢的問題,但daemonize.rb文件在哪裏?它是一顆寶石,在網上我能找到它的地方,它是標準庫,還是什麼? – agentbanks217 2010-09-10 23:05:24
這是一顆寶石。你只需安裝並開始使用它。 – zengr 2010-09-10 23:13:08
好的非常感謝。 – agentbanks217 2010-09-10 23:51:07