您的位置:首页 > 运维架构 > Linux

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

2016-05-17 16:33 701 查看
参考文章: 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)


搞定, 下班!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: