歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux應用程序以服務方式(Service)運行,並且保證死機能重啟。

Linux應用程序以服務方式(Service)運行,並且保證死機能重啟。

日期:2017/3/3 12:42:10   编辑:Linux技術
參考文章: http://blog.terminal.com/using-daemon-to-daemonize-your-programs/
ubuntu 自帶了一個daemon 程序, 執行 apt-get install daemon,
然後就被安裝到了 /usr/bin/daemon,
下面我們創建一個測試腳本:
#!/bin/bash
echo $(date)" Starting Script" >> /tmp/output  
while true  
        do
        echo $(date) >> /tmp/output
        sleep 5
done

運行:
#daemon -r /root/test.sh

我們發現後台可以啟動了兩個進程:
[root@terminal40162 ~] ps -ef | grep test
root     11421     1  0 14:11 ?        00:00:00 daemon -r /root/test.sh  
root     11422 11421  0 14:11 ?        00:00:00 /bin/bash /root/test.sh

當執行 kill 11422 之後, /bin/bash /root/test.sh 這個進程又冒出來了。
這就是deamon 在後台監控起的作用。
接著測試服務啟動,停止,重啟:
service testd start
service testd stop
service testd restart

該服務加入開啟啟動(UBUNTU貌似無效):
chkconfig --add testd

檢查是否已經設置成功:
[root@lvs01 tmp]# chkconfig --list testd

testd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

ubuntu貌似無該命令 chkconfig, 不過不用著急,可以手動添加軟鏈接:
ln  /etc/init.d/testd  /etc/rc0.d/S0test -s
ln  /etc/init.d/testd  /etc/rc1.d/S0test -s
ln  /etc/init.d/testd  /etc/rc2.d/S0test -s
ln  /etc/init.d/testd  /etc/rc3.d/S0test -s
ln  /etc/init.d/testd  /etc/rc4.d/S0test -s
ln  /etc/init.d/testd  /etc/rc5.d/S0test -s
ln  /etc/init.d/testd  /etc/rc6.d/S0test -s

重啟ubuntu, 服務已經啟動:
root@dns066:~# service testd status
daemon:  TEST is running (pid 1106)

搞定, 下班!
Copyright © Linux教程網 All Rights Reserved