daemontools管理前台服务:自动失败重启,获取程序日志
文章目录
- 1,编译安装
- 2,基本使用
3,测试:前台运行的服务
自定义后台服务简单脚本(监控失败并重启) : https://blog.csdn.net/eyeofeagle/article/details/94485522
- daemontools功能介绍
1,编译安装
源码下载:http://cr.yp.to/daemontools/install.html
编译好的二进制包下载 (提取码:abcd):https://pan.baidu.com/s/1v3BfNn-mOPytM5_k4A3FSQ
- 解压后,package/install 运行报错,需要添加如下配置:参考 https://blog.csdn.net/qq_26707177/article/details/83988970
- 开机启动服务:
echo '/command/svscanboot &' >> /etc/rc.local ; chmod +x /etc/rc.local
run 脚本文件:需要可执行权限,格式必须规范( 以
#!/bin/bash
开头)安装后的目录结构(/service, /command —> /opt/admin/daemontools/command/)
mkdir /service; chmod 1755 /service; chmod 1755 /service
ln -sf /opt/admin/daemontools/command/ /command; ln -sf /command/* /usr/local/bin/
[root@c7-docker ~]# ls /opt/admin/ -l
total 4
lrwxrwxrwx. 1 root root 16 Apr 6 18:39 daemontools -> daemontools-0.76
drwxr-xr-x. 6 root root 4096 Mar 25 10:29 daemontools-0.76
[root@c7-docker ~]# ls /opt/admin/daemontools
command compile package src
[root@c7-docker ~]# ls /opt/admin/daemontools/command/
envdir fghack pgrphack setlock softlimit svc svscan svstat tai64nlocal
envuidgid multilog readproctitle setuidgid supervise svok svscanboot tai64n
[root@c7-docker ~]# ls /command -l
lrwxrwxrwx. 1 root root 31 Apr 6 18:55 /command -> /opt/admin/daemontools/command/
#环境变量-链接
[root@c7-docker ~]# which svc
/usr/local/bin/svc
[root@c7-docker ~]# ll /usr/local/bin/svc
lrwxrwxrwx. 1 root root 12 Apr 6 19:05 /usr/local/bin/svc -> /command/svc
[root@c7-docker ~]# ll /usr/local/bin/svscanboot
lrwxrwxrwx. 1 root root 19 Apr 6 19:05 /usr/local/bin/svscanboot -> /command/svscanboot
2,基本使用
#---1,管理服务:停止,重启
#Easy, reliable signalling. With /service, the system administrator can use svc to control your daemon. For example:
svc -o /service/yourdaemon: runs the service once
#重启服务
svc -t /service/yourdaemon: sends TERM, and automatically restarts the daemon after it dies
#停止服务
svc -h /service/yourdaemon: sends HUP
svc -d /service/yourdaemon: sends TERM, and leaves the service down
#启动服务
svc -u /service/yourdaemon: brings the service back up
#---2,状态查看
[root@c7-docker ~]# svstat /service/test1
/service/test1: up (pid 17014) 0 seconds
[root@c7-docker ~]# ps -ef |grep svs
root 791 1 0 08:44 ? 00:00:00 /bin/sh /command/svscanboot
root 795 791 0 08:44 ? 00:00:00 svscan /service
root 17305 1559 0 09:19 pts/0 00:00:00 grep --color=auto svs
#查看管理的服务和 pid号
[root@c7-docker ~]# pstree -a -p 791
svscanboot,791 /command/svscanboot
├─readproctitle,796 service errors:...
└─svscan,795 /service
└─supervise,805 test1
└─(date,17430)
#---3,修改被管理的服务: How do I upgrade a service directory? I need to change ./run.
Create ./run.new and atomically rename ./run.new to ./run. Then use
svc -t .
to send a TERM signal to the daemon. supervise will start the new ./run after the daemon exits.
3,测试:前台运行的服务
#配置服务(前台允许的,不是后台运行),并获取日志
####### 1,启动服务
[root@c7-docker ~]# cat /service/aaa/run
#!/bin/bash
#exec 2>&1
#exec envuidgid user1 envdir ./env softlimit -d300000 command.....
exec date
[root@c7-docker ~]# svc -u /service/aaa/
####### 2,启动日志获取 (需要手动创建 log/main日志目录)
[root@c7-docker ~]# cat /service/aaa/log/run
#!/bin/bash
#exec setuidgid user1 multilog t ./main
exec multilog t ./main
[root@c7-docker ~]# mkdir -p /service/aaa/log/main/
[root@c7-docker ~]# svc -u /service/aaa/log/
# 查看程序运行日志
[root@c7-docker ~]# tail -n2 /service/aaa/log/main/
@4000000060750f7529f9b84c.u current lock state
[root@c7-docker ~]# tail -n2 /service/aaa/log/main/current
@40000000607539a23840ac44 Tue Apr 13 14:26:32 CST 2021
@40000000607539a33aa2c7c4 Tue Apr 13 14:26:33 CST 2021
####### 3,配置自动管理tomcat重启
[root@c7-docker opt]# cat /service/tomcat/run
#!/bin/bash
exec 2>&1
#exec setuidgid vagrant whoami
#exec setuidgid vagrant java -jar /opt/com.example.demo-0.0.1-SNAPSHOT.jar
exec setuidgid vagrant /opt/apache-tomcat-8.5.54/bin/catalina.sh run
[root@c7-docker opt]# cat /service/tomcat/log/run
#!/bin/bash
#exec setuidgid vagrant multilog t ./main
exec setuidgid vagrant multilog ./main
[root@c7-docker ~]# mkdir -p /service/tomcat/log/main ; chown -R vagrant /service/tomcat/log/
[root@c7-docker ~]# svc -u /service/tomcat
[root@c7-docker ~]# svc -u /service/tomcat/log/
####### 4,查看进程状态
[root@test-c62 opt]# ps -ef |grep svs
root 7176 17071 0 19:28 pts/0 00:00:00 /bin/sh /usr/local/bin/svscanboot
root 7178 7176 0 19:28 pts/0 00:00:00 svscan /service
root 18329 17071 0 19:50 pts/0 00:00:00 grep svs
[root@c7-docker opt]# pstree -ap 7176
svscanboot,7176 /usr/local/bin/svscanboot
├─readproctitle,7179 service errors:...
└─svscan,7178 /service
├─supervise,7180 aaa
│ └─(date,18330)
├─supervise,7181 log
│ └─multilog,7185 t ./main
├─supervise,7182 tomcat
│ └─java,18131 -Djava.util.logging.config.file=/opt/apache-tomcat-8.5.54/conf/logging.properties...
│ ├─{ java},18142
│ ├─{ java},18143
│ ├─{ java},18158
│ └─{ java},18160
└─supervise,7183 log
└─multilog,11207 ./main
还没有评论,来说两句吧...