來源
同事
問題場景
- 同步任務(wù)開始的時候設(shè)置一個標(biāo)記谤牡,放到redis实柠,結(jié)束的時候刪除;進程被kiil -9之后拯杠,redis緩存沒有刪除掏婶;重啟應(yīng)用之后,一直認為在同步中
思考
- redis緩存設(shè)置時間
- 如何優(yōu)雅的停機
研究優(yōu)雅停機時的一點思考
項目實踐
停止腳本 使用kill -15潭陪;而不是kill -9
- kill -15 獻給應(yīng)用一段時間善后雄妥,這時候Spring容器會正常關(guān)閉,執(zhí)行 @PreDestroy 等類似方法
原先
stop_application() {
echo "stop jar"
if [ -f "$PID_FILE" ]; then
kill -9 `cat $PID_FILE`
rm $PID_FILE
else
echo "pid file $PID_FILE does not exist, do noting"
fi
}
修改后
stop_application() {
echo "stop jar"
if [ -f "$PID_FILE" ]; then
kill -15 `cat $PID_FILE`
else
echo "pid file $PID_FILE does not exist, do noting"
fi
SLEEP=5
FORCE=1
if [ ! -z "$PID_FILE" ]; then
if [ -f "$PID_FILE" ]; then
while [ $SLEEP -ge 0 ]; do
kill -0 `cat "$PID_FILE"` >/dev/null 2>&1
if [ $? -gt 0 ]; then
rm -f "$PID_FILE" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ -w "$PID_FILE" ]; then
cat /dev/null > "$PID_FILE"
# If application has stopped don't try and force a stop with an empty PID file
FORCE=0
else
echo "The PID file could not be removed or cleared."
fi
fi
echo "application stopped."
break
fi
if [ $SLEEP -gt 0 ]; then
sleep 1
fi
if [ $SLEEP -eq 0 ]; then
echo "application did not stop in time."
if [ $FORCE -eq 0 ]; then
echo "PID file was not removed."
fi
echo "To aid diagnostics a thread dump has been written to standard out."
kill -3 `cat "$PID_FILE"`
fi
SLEEP=`expr $SLEEP - 1 `
done
fi
else
echo "application normal stopped."
rm -f "$PID_FILE" >/dev/null 2>&1
fi
##強制殺死進程
KILL_SLEEP_INTERVAL=5
if [ $FORCE -eq 1 ]; then
if [ -z "$PID_FILE" ]; then
echo "Kill failed: \$PID_FILE not set"
else
if [ -f "$PID_FILE" ]; then
PID=`cat "$PID_FILE"`
echo "killing application with the PID: $PID force"
kill -9 $PID
while [ $KILL_SLEEP_INTERVAL -ge 0 ]; do
kill -0 `cat "$PID_FILE"` >/dev/null 2>&1
if [ $? -gt 0 ]; then
rm -f "$PID_FILE" >/dev/null 2>&1
if [ $? != 0 ]; then
if [ -w "$PID_FILE" ]; then
cat /dev/null > "$PID_FILE"
else
echo "The PID file could not be removed."
fi
fi
echo "The application process has been killed."
break
fi
if [ $KILL_SLEEP_INTERVAL -gt 0 ]; then
sleep 1
fi
KILL_SLEEP_INTERVAL=`expr $KILL_SLEEP_INTERVAL - 1 `
done
if [ $KILL_SLEEP_INTERVAL -lt 0 ]; then
echo "application has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."
fi
fi
fi
fi
}
JAVA代碼
Spring容器關(guān)閉的鉤子
- Spring已經(jīng)自動幫我們加了鉤子
org.springframework.context.support.AbstractApplicationContext#registerShutdownHook
@Override
public void registerShutdownHook() {
if (this.shutdownHook == null) {
// No shutdown hook registered yet.
this.shutdownHook = new Thread() {
@Override
public void run() {
synchronized (startupShutdownMonitor) {
doClose();
}
}
};
Runtime.getRuntime().addShutdownHook(this.shutdownHook);
}
}
- 實現(xiàn)下面這些依溯,Sping容器關(guān)閉的時候會自動處理
- @PreDestroy注解
- destory-method方法
- DisposableBean接口
也可以自己加其他的鉤子
Runtime.getRuntime().addShutdownHook(this.shutdownHook);
觸發(fā)了 linux OOM KILLER老厌,還會不會執(zhí)行鉤子?
應(yīng)該不會黎炉,屬于強殺
- 這是linux oom kill 的代碼
強行翻譯:
為了防止OOM受害者耗盡 在它控制下的 從用戶空間那保留的 內(nèi)存枝秤,我們應(yīng)該 在授權(quán)它訪問保留的內(nèi)存 之前發(fā)送SIGKILL
/*
* We should send SIGKILL before granting access to memory reserves
* in order to prevent the OOM victim from depleting the memory
* reserves from the user space under its control.
*/
do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
- 代碼中的SIGKILL
https://linux.cn/article-8771-1.html