RaspberryPi æ°ã«å ¥ã£ã¦ä½¿ã£ã¦ã¾ããDebianãå ¥ãã¦ã¾ããraspbian ã§ããã
RaspberryPiãupstartæªå°å ¥ã ã£ãã®ã§ruby ã§initã¹ã¯ãªããä½ã£ã
daemonsããæ軽
gem install daemons
Process.daemonizeãããæ¨ä»ã«ããã¦daemonsããã±ã¼ã¸ã¯ä¸è¦ãã¨æã£ã¦ãããã©ãã¾ã ã¾ã 使ããã
start/stop/resutart/run ãããã«ä½ãã
daemonsããã±ã¼ã¸ä½¿ãã¨ãinitã¹ã¯ãªããã«å¿ è¦ãªã³ãã³ãå¼æ°ãããã«ä½ã£ã¦ãããã
Daemons.run_proc ã« Procã渡ãã ã
require "daemons" Daemons.run_proc("my_first_proc_daemon", options ){ begin server = Thread.new{ :job_server } woker = Thread.new { :job_worker } server.start server.join rescue #retry end }
èµ·åçµäº
./my_job_queue start ./my_job_queue stop ./my_job_queue status ./my_job_queue run # foreground ã« stay on topãã
RaspberryPi ã® init.d daemon ã«ãã
:dir_mode => :system ã渡ãã¦ã /var/run ã« pidãã¡ã¤ã«ãä½æããããã«ããã
ãã¨ã¯ããã®ãã¡ã¤ã«ã /etc/init.d ã«ã³ãã¼ãã¦ãé©å½ãªRunlevelãè²¼ãä»ããã°OK
é©å½ãªã©ã³ã¬ãã«
runlevel ãè²¼ãã«ã¯ã /etc/init.d ã«ãããã¹ã¯ãªããã /etc/rc{0-5}.d/ã«ã¨ã¤ãªã¢ã¹ããã°ãããæ°å¤ã¯èµ·åé åºã
é¢åãªã¨ãã¯ãrcconf ã§ããã£ã¨ã
åºæ¥ä¸ãã£ãinit.dã¹ã¯ãªãã
#!/usr/bin/env ruby ### BEGIN INIT INFO # Provides: my_server # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: my_server # Description: my_server ### END INIT INFO require 'daemons' options ={ :dir_mode => :system, ##/var/runã« :backtrace => true, :monitor =>true, :log_dir => "/var/log/my_server", :log_output => true, } Daemons.run_proc("smtp_proxy_server", options ){ server = Thread.new{ loop {sleep 1;print :awake;} } server.pass server.join }
ããã»ã¹ç£è¦ãã:monitor
ã¾ãã:monitor=>trueã«ããã¨ãããã»ã¹ãErrorã§æ»ãã æã«åèµ·åãèªåã§è¡ã£ã¦ããã¦æ´ã«ä¾¿å©ã
options ={ :monitor=>true, ##以ä¸ç¥ }
monitorã便å©ã§ãã
monitorãç¡äºèµ·åããã¨ãããã»ã¹ãã²ã¨ã¤å¢ããã
takuya@raspberrypi:~$ ps auxf | grep smtp root 2655 0.0 1.8 17684 9052 ? Sl 04:51 0:06 smtp_proxy_server_monitor root 2662 0.0 1.3 13880 6760 ? Sl 04:51 0:04 smtp_proxy_server
smtp forwarder ã ruby ã§ä½ã£ã¦èµ·åãç£è¦ãã¦ãã¾ãã
INIT INFO ã«ã¤ãã¦ã¯
INITã¹ã¯ãªããã¯ãLSB (Linux Standard base ) ã§ãããæ¸ãããæå®ããã¦ããã®ã§ããéãã«ã
### BEGIN INIT INFO # Provides: my_server # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: my_server # Description: my_server ### END INIT INFO
大äºãªã®ã¯ Required-Start ã§ãããã$allä»¥å¤ ã«ããã¨ããããã¯ã¼ã¯ããã¡ã¤ã«ã·ã¹ãã ãèµ·åæç¹ã§èµ·åããã¨ãè²ã 決ããããããã¾ã¯ $all ãªã®ã§å ¨é¨ã®èµ·åå¦çå¾ã«ã¹ã¯ãªãããDaemonizeãã¦ãã
https://wiki.debian.org/LSBInitScripts
upstart ãããã°ããã
upstart ãã¢ã¬ã°ãã£ã¨æ¥½ã«æ¸ãããã ãã©ãèªåã§èµ·åçµäºã管çããããã¨ãããã£ã¦ãã¨ã¨ã raspberryPiã®Debianã«UpStartå ¥ã£ã¦ãªãããã§ãã apt-getã§ã¤ã³ã¹ãã¼ã«åºæ¥ããã ãã©ãå ¥ãããä½ãçªç¶èª¤ä½åãããæãã®ã§ã