1.最簡單的自動重啟范例
[Unit] Description=mytest [Service] Type=simple ExecStart=/root/mytest.sh Restart=always RestartSec=5 StartLimitInterval=0 [Install] WantedBy=multi-user.target
重點參數詳解
-
Restart=always: 只要不是通過systemctl stop來停止服務,任何情況下都必須要重啟服務,默認值為no
-
RestartSec=5: 重啟間隔,比如某次異常后,等待5(s)再進行啟動,默認值0.1(s)
-
StartLimitInterval: 無限次重啟,默認是10秒內如果重啟超過5次則不再重啟,設置為0表示不限次數重啟
2.案例需求
需求:有個業務,當程序因受到OOM而退出的時候,不希望自動重啟(此時需要人工介入排查),其他情況下可以自動重啟
分析:OOM就是通過kill -9來殺進程,因此只要找到方法,告訴systemd當該服務遇到kill -9時候不自動重啟即可
3.RestartPreventExitStatus參數
查詢man systemd.service發現,systemd的[Service]段落里支持一個參數,叫做RestartPreventExitStatus
該參數從字面上看,意思是當符合某些退出狀態時不要進行重啟。
該參數的值支持exit code和信號名2種,可寫多個,以空格分隔,例如
-
RestartPreventExitStatus=143 137 SIGTERM SIGKILL
表示,當退出情況只要符合以下4種情況中任意一種時候,則不再進行重啟
-
exit code為143
-
exit code為137
-
信號為TERM
-
信號為KILL
但具體如何使用,請繼續往下看
4.測試方法
/usr/lib/systemd/system/mytest.service
[Unit] Description=mytest [Service] Type=simple ExecStart=/root/mem Restart=always RestartSec=5 StartLimitInterval=0 RestartPreventExitStatus=SIGKILL [Install] WantedBy=multi-user.target
/root/mem.c(不斷消耗內存直至發生OOM)
#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main () { char *p = NULL; int count = 1; while(1){ p = (char *)malloc(1024*1024*100); if(!p){ printf("malloc error!n"); return -1; } memset(p, 0, 1024*1024*100); printf("malloc %dM memoryn", 100*count++); usleep(500000); } }
編譯及執行
gcc -o /root/mem /root/mem.c systemctl daemon-reload systemctl start mytest
5.測試結果
[root@fzxiaomange ~]# systemctl status mytest ● mytest.service - mytest Loaded: loaded (/usr/lib/systemd/system/mytest.service; disabled; vendor preset: disabled) Active: failed (Result: signal) since Sat 2018-10-20 23:32:24 CST; 45s ago Process: 10555 ExecStart=/root/mem (code=killed, signal=KILL) Main PID: 10555 (code=killed, signal=KILL) Oct 20 23:31:55 fzxiaomange.com systemd[1]: Started mytest. Oct 20 23:31:55 fzxiaomange.com systemd[1]: Starting mytest... Oct 20 23:32:24 fzxiaomange.com systemd[1]: mytest.service: main process exited, code=killed, status=9/KILL Oct 20 23:32:24 fzxiaomange.com systemd[1]: Unit mytest.service entered failed state. Oct 20 23:32:24 fzxiaomange.com systemd[1]: mytest.service failed.
重點看上面第6行
MainPID:10555(code=killed,signal=KILL)
,這行表示主進程的狀態,常見有2種情況
code=exited, status=143:表示systemd認為主進程自行退出的,exit code為143
code=killed, signal=KILL:表示systemd認為主進程是被kill的,接收到的信號是SIGKILL
等待5秒后,並沒有自動重啟,符合預期
此時將RestartPreventExitStatus=SIGKILL改為RestartPreventExitStatus=SIGTERM
執行systemctl restart mytest,再進行一次觀察,等待5秒后,服務自動重啟,符合預期
6.注意事項
6.1.RestartPreventExitStatus與Restart的關系
配置RestartPreventExitStatus=后,並沒有完全忽略Restart=,而是指當退出情況與RestartPreventExitStatus=匹配的時候,才忽略Restart=,若沒有匹配,根據Restart=該怎么樣還怎么樣(具體詳見后面的詳細測試數據)
6.2.kill子進程會是什么情況
若systemd啟動的不是一個簡單進程,而是會派生子進程的情況(比如執行shell腳本,shell腳本里啟動多個程序),那么當另外開一個窗口通過 kill-信號
測試時,會是什么情況呢,先貼出測試方法
ExecStart=/root/mem改為ExecStart=/root/mytest.sh
/root/mytest.sh內容為
-
#!/bin/bash
-
sleep 100000 &
-
sleep 200000
測試結果
-
若kill 主進程PID(kill不帶參數),則主進程狀態為
code=killed,signal=TERM
-
若kill -9 主進程PID,則主進程狀態為
code=killed,signal=KILL
-
若kill 最后一個子進程PID(kill不帶參數),則systemd不認為是接收到信號,而是根據最后一個進程的exit code進行處理,此時主進程狀態為
code=exited,status=143
-
若kill -9 最后一個子進程PID,此時主進程狀態為
code=exited,status=137
7.詳細測試數據
上面有提到RestartPreventExitStatus和Restart的關系,但沒有數據說明
另外,kill和kill -9的區別,也需要有一份數據說明
因此做了一個詳細對比,這里附上詳細數據
轉自
systemd實踐: 依據情況自動重啟服務 – 小慢哥的技術網站 https://fzxiaomange.com/2018/10/21/systemd-restartpreventexitstatus/
技術|Systemd服務簡介 https://linux.cn/article-3352-3.html