daemontools管理前台服务:自动失败重启,获取程序日志

快来打我* 2022-11-18 13:47 152阅读 0赞

文章目录

  • 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/

  1. #环境变量-链接
  2. [root@c7-docker ~]# which svc
  3. /usr/local/bin/svc
  4. [root@c7-docker ~]# ll /usr/local/bin/svc
  5. lrwxrwxrwx. 1 root root 12 Apr 6 19:05 /usr/local/bin/svc -> /command/svc
  6. [root@c7-docker ~]# ll /usr/local/bin/svscanboot
  7. lrwxrwxrwx. 1 root root 19 Apr 6 19:05 /usr/local/bin/svscanboot -> /command/svscanboot

2,基本使用

  1. #---1,管理服务:停止,重启
  2. #Easy, reliable signalling. With /service, the system administrator can use svc to control your daemon. For example:
  3. svc -o /service/yourdaemon: runs the service once
  4. #重启服务
  5. svc -t /service/yourdaemon: sends TERM, and automatically restarts the daemon after it dies
  6. #停止服务
  7. svc -h /service/yourdaemon: sends HUP
  8. svc -d /service/yourdaemon: sends TERM, and leaves the service down
  9. #启动服务
  10. svc -u /service/yourdaemon: brings the service back up
  11. #---2,状态查看
  12. [root@c7-docker ~]# svstat /service/test1
  13. /service/test1: up (pid 17014) 0 seconds
  14. [root@c7-docker ~]# ps -ef |grep svs
  15. root 791 1 0 08:44 ? 00:00:00 /bin/sh /command/svscanboot
  16. root 795 791 0 08:44 ? 00:00:00 svscan /service
  17. root 17305 1559 0 09:19 pts/0 00:00:00 grep --color=auto svs
  18. #查看管理的服务和 pid号
  19. [root@c7-docker ~]# pstree -a -p 791
  20. svscanboot,791 /command/svscanboot
  21. ├─readproctitle,796 service errors:...
  22. └─svscan,795 /service
  23. └─supervise,805 test1
  24. └─(date,17430)
  25. #---3,修改被管理的服务: How do I upgrade a service directory? I need to change ./run.
  26. Create ./run.new and atomically rename ./run.new to ./run. Then use
  27. svc -t .
  28. to send a TERM signal to the daemon. supervise will start the new ./run after the daemon exits.

3,测试:前台运行的服务

  1. #配置服务(前台允许的,不是后台运行),并获取日志
  2. ####### 1,启动服务
  3. [root@c7-docker ~]# cat /service/aaa/run
  4. #!/bin/bash
  5. #exec 2>&1
  6. #exec envuidgid user1 envdir ./env softlimit -d300000 command.....
  7. exec date
  8. [root@c7-docker ~]# svc -u /service/aaa/
  9. ####### 2,启动日志获取 (需要手动创建 log/main日志目录)
  10. [root@c7-docker ~]# cat /service/aaa/log/run
  11. #!/bin/bash
  12. #exec setuidgid user1 multilog t ./main
  13. exec multilog t ./main
  14. [root@c7-docker ~]# mkdir -p /service/aaa/log/main/
  15. [root@c7-docker ~]# svc -u /service/aaa/log/
  16. # 查看程序运行日志
  17. [root@c7-docker ~]# tail -n2 /service/aaa/log/main/
  18. @4000000060750f7529f9b84c.u current lock state
  19. [root@c7-docker ~]# tail -n2 /service/aaa/log/main/current
  20. @40000000607539a23840ac44 Tue Apr 13 14:26:32 CST 2021
  21. @40000000607539a33aa2c7c4 Tue Apr 13 14:26:33 CST 2021
  22. ####### 3,配置自动管理tomcat重启
  23. [root@c7-docker opt]# cat /service/tomcat/run
  24. #!/bin/bash
  25. exec 2>&1
  26. #exec setuidgid vagrant whoami
  27. #exec setuidgid vagrant java -jar /opt/com.example.demo-0.0.1-SNAPSHOT.jar
  28. exec setuidgid vagrant /opt/apache-tomcat-8.5.54/bin/catalina.sh run
  29. [root@c7-docker opt]# cat /service/tomcat/log/run
  30. #!/bin/bash
  31. #exec setuidgid vagrant multilog t ./main
  32. exec setuidgid vagrant multilog ./main
  33. [root@c7-docker ~]# mkdir -p /service/tomcat/log/main ; chown -R vagrant /service/tomcat/log/
  34. [root@c7-docker ~]# svc -u /service/tomcat
  35. [root@c7-docker ~]# svc -u /service/tomcat/log/
  36. ####### 4,查看进程状态
  37. [root@test-c62 opt]# ps -ef |grep svs
  38. root 7176 17071 0 19:28 pts/0 00:00:00 /bin/sh /usr/local/bin/svscanboot
  39. root 7178 7176 0 19:28 pts/0 00:00:00 svscan /service
  40. root 18329 17071 0 19:50 pts/0 00:00:00 grep svs
  41. [root@c7-docker opt]# pstree -ap 7176
  42. svscanboot,7176 /usr/local/bin/svscanboot
  43. ├─readproctitle,7179 service errors:...
  44. └─svscan,7178 /service
  45. ├─supervise,7180 aaa
  46. └─(date,18330)
  47. ├─supervise,7181 log
  48. └─multilog,7185 t ./main
  49. ├─supervise,7182 tomcat
  50. └─java,18131 -Djava.util.logging.config.file=/opt/apache-tomcat-8.5.54/conf/logging.properties...
  51. ├─{ java},18142
  52. ├─{ java},18143
  53. ├─{ java},18158
  54. └─{ java},18160
  55. └─supervise,7183 log
  56. └─multilog,11207 ./main

发表评论

表情:
评论列表 (有 0 条评论,152人围观)

还没有评论,来说两句吧...

相关阅读

    相关 docker 服务、Docker

    [前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。][Link 1] 1. 我的情况 :重启阿里云 ECS 服务器后,一切服