Intro
背景: binary tar 包的 tomcat 需要一个守护进程完成开机自启,以及服务意外退出的守护启动。
解决思路
supervisord 和 systemd 对比
比对了 supervisor 和 systemd,自动重启部分 systemd 更加满足日常运维的需要。
关于 Tomcat 的 systemd 参考:
How To Install Apache Tomcat 8 on CentOS 7
但是当前使用的是 Ubuntu 14.04,默认没有 systemd 服务。
以下内容参考链接: 如何在 Ubuntu 14.04中使用systemctl?
我刚刚自己遇到了这个问题,发现Ubuntu 14.04使用Upstart而不是Systemd,所以systemctl命令不起作用。
Ubuntu Startup
Ubuntu 有很多不同的配置开机自启动的方式,/etc/init.d/ 和 /etc/init 目录的保留就是为了版本的上下兼容
以下内容参考链接: ubuntu的service,update-rc.d和systemctl命令
init类型 | 说明 | 识别 |
---|---|---|
Systemd | 新出现的init,很多linux发行版都已经或者计划转向Systemd | 如果你的系统目录文件中/usr/lib/systemd和/etc/systemd,说明你有systemd,例如我的ubuntu 17.10就有 |
Upstart | ubuntu曾经使用的init,但是现在已经转向了systemd | 系统目录有/etc/init,而且其中有许多conf文件,说明你的系统有upstart(除非你的系统是debian7,那说明你使用的是System V init),同时,我发现我的ubuntu 17.10也有这个目录,我猜测可能是为了兼容(此处存疑问,现在的ubuntu的init确实是systemd) |
System V init | 传统的init,大多数的linux发行版都会兼容 | 系统有/etc/inittab文件,说明你很可能是System V init |
理解备注:
- System V init 就是 service,也就是 /etc/init.d 下的脚本
- update-rc.d 一般是在不同的运行级别 runlevel 下创建软链接:
/etc/rc[runlevel].d/NNname => /etc/init.d/name
,可以通过运行runlevel
命令来查看当前的运行级别,然后到对应的/etc/rc[runlevel].d/
下找到可配置自启动的项
以下内容参考链接: ubuntu 14.04 /etc/init.d/ vs /etc/init/ start service at startup
It is true that /etc/init was introduced by Upstart, while /etc/init.d goes back to SysV. However, my point was that /etc/init.d contains scripts to be executed, while /etc/init contains configuration. But, upstart is dead anyway, and systemd introduces /etc/systemd. Note that /etc/init as well as /etc/init.d still exist on current Ubuntus (15.04 here), but I guess these are just transitional files installed by packages that support all three init systems.
以下内容参考链接: Ubuntu 系统 Update-rc.d 命令
Ubuntu中的运行级别
0(关闭系统)
1(单用户模式,只允许root用户对系统进行维护。)
2 到 5(多用户模式,其中3为字符界面,5为图形界面。)
6(重启系统)
以下内容参考链接:Ubuntu startup – init scripts, runlevels, upstart jobs explained
Run level | Name | Description |
---|---|---|
1 | Single-user mode | Mode for administrative tasks. |
2 | Multi-User Mode | Does not configure network interfaces and does not export networks services |
3 | Multi-User Mode with Networking | Starts the system normally |
4 | Not used / user definable | For special purposes |
5 | Start the system normally with GUI display manager | Run level 3 + display manager |
6 | Reboot | Reboots the system |
s or S | Single-user mode | Does not configure network interfaces, or start daemons. |
0 | Halt | Shuts down the system |
supervisord
确定使用 supervisor 自启,然后维护 Tomcat 程序作为最终解决方法。
确保 supervisor 开机自启,update-rc.d supervisor defaults
是相对简单的配置开机自启的方式,有更多 runlevel 的改变的话,可以再查 update-rc.d 的相关设置文档。
查看 supervisord 的配置文件 /etc/supervisor/supervisord.conf,最后一行是
[include]
files = /etc/supervisor/conf.d/*.conf
所以,可以直接操作 /etc/supervisor/conf.d 目录下以 conf 作为尾缀的配置文件
以下内容参考链接: jenkins安装部署-用supervisord守护进程自启动
# /etc/supervisor/conf.d/tomcat.conf
[program:jenkins] # 随便起一个程序名
command=/home/eng-admin/apache-tomcat-8.5.23/bin/catalina.sh run # 检查 / 启动的脚本
numprocs=1 # 启动进程的数目
user=eng-admin
directory=/home/eng-admin/apache-tomcat-8.5.23/bin
autostart=true # 跟随 supervisor 开机自启
autorestart=true # 自动重启
startretries=30 # 重启最多次数
startsecs=30 # 每次重启间隔时间
最后,重新加载 supervisord 配置文件即可
以下内容参考链接: supervisor更改某项目配置后 需要重新启动才有效
# 更新新的配置到 supervisord
supervisorctl update
# 重新启动配置中的所有程序
supervisorctl reload