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 下的腳本
- Upstart 一般是在不同的運行級別 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/check_jenkins_tomcat.sh # 檢查並啓動的腳本
numprocs=1 # 啓動進程的數目
user=eng-admin
directory=/home/eng-admin/apache-tomcat-8.5.23/bin
autostart=true # 跟隨 supervisor 開機自啓
autorestart=true # 自動重啓
startretries=30 # 重啓最多次數
startsecs=30 # 每次重啓間隔時間
redirect_stderr=true
stdout_logfile=logfile
以下內容參考鏈接: 用shell腳本監控進程是否存在 不存在則啓動的實例
# /home/eng-admin/check_jenkins_tomcat.sh
#!/bin/bash
ps -ef |grep java |grep cata
if [ $? -ne 0 ]
then
cd /home/eng-admin/apache-tomcat-8.5.23/bin/; sh ./startup.sh
else
echo "runing....."
fi
最後,重新加載 supervisord 配置文件即可
以下內容參考鏈接: supervisor更改某項目配置後 需要重新啓動纔有效
# 更新新的配置到 supervisord
supervisorctl update
# 重新啓動配置中的所有程序
supervisorctl reload