Linux 的 Apache / Nginx 都有類似的問題,就是當 Process 掛掉(不管是自己掛還是被打掉),想要自動重新啟動,要怎麼做?
以前會使用 monit 來做,現在有 Systemd 就可以使用系統預設得來達成
Linux Apache 設定 Systemd 掛掉自動重啟
先直接修改 Apache 的 Systemd 設定檔
- sudo vim /etc/systemd/system/multi-user.target.wants/apache2.service # 修改下述
#Restart=on-abort
Restart=always - sudo systemctl daemon-reload
- sudo systemctl restart apache2
雖然上述是正常的,但是 Apache 一升級,會發現設定檔就被蓋掉了,於是需要使用 systemctl edit 來修改設定檔:
- sudo systemctl edit apache2 # 前面很多註解,寫法仿造註解寫法做(會寫入暫存 /etc/systemd/system/apache2.service.d/override.conf) # 修改下述
# #Restart=on-abort
# Restart=always
# RestartSec=3s - sudo systemctl daemon-reload
- sudo systemctl restart apache2
- sudo systemctl status apache2 # 查看看狀態
死掉自動啟動的測試
- ps aux | grep apache2
- sudo killall apache2
- ps aux | grep apache2 # 3秒後 Apache 是否正常啟動回來