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,
下面我们创建一个测试脚本:
运行:
我们发现后台可以启动了两个进程:
当执行 kill 11422 之后, /bin/bash /root/test.sh 这个进程又冒出来了。
这就是deamon 在后台监控起的作用。
接着测试服务启动,停止,重启:
该服务加入开启启动(UBUNTU貌似无效):
检查是否已经设置成功:
ubuntu貌似无该命令 chkconfig, 不过不用着急,可以手动添加软链接:
重启ubuntu, 服务已经启动:
搞定, 下班!
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)
搞定, 下班!
相关文章推荐
- linux 流量控制全攻略(TC)
- Linux终端下 dstat 监控工具
- Linux命令扫盲 之 sar
- Linux下C程序的链接过程
- LINUX学习之路(学LINUX必看)
- linux 截包
- Linux下NDK编译FFMPEG包含neon参数
- centos下安装cassandra3.5
- Linux学习笔记:SQLyog链接linux虚拟机上的mysql数据库
- Centos安装mysql.rpm
- Linux系统修改编码
- Linux(ubuntu)上安装RDP Server(Xrdp)使用的注意事项
- Linux中安装jdk1.6 x64步骤详解
- Centos安装firefox/chrome
- 几种在Linux下查询外网IP的办法
- 一段关于Unix与 Linux的暗黑史
- linux下搭建DNS子域及相关授权详解
- centos7 & mysql
- centos ip配置
- Linux简介