Linux应用程序以服务方式(Service)运行,并且保证死机能重启。

参考文章:  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)



搞定, 下班!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值