kill -9影響&如何優(yōu)雅停機

來源

同事

問題場景

  • 同步任務開始的時候設置一個標記,放到redis,結束的時候刪除;進程被kiil -9之后,redis緩存沒有刪除;重啟應用之后,一直認為在同步中

思考

  • redis緩存設置時間
  • 如何優(yōu)雅的停機

研究優(yōu)雅停機時的一點思考

研究優(yōu)雅停機時的一點思考

項目實踐

停止腳本 使用kill -15;而不是kill -9

  • kill -15 獻給應用一段時間善后,這時候Spring容器會正常關閉,執(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容器關閉的鉤子

  1. 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);
        }
    }
  1. 實現(xiàn)下面這些,Sping容器關閉的時候會自動處理
  • @PreDestroy注解
  • destory-method方法
  • DisposableBean接口

也可以自己加其他的鉤子

Runtime.getRuntime().addShutdownHook(this.shutdownHook);

觸發(fā)了 linux OOM KILLER,還會不會執(zhí)行鉤子?

應該不會,屬于強殺

  • 這是linux oom kill 的代碼

https://github.com/torvalds/linux/blob/master/mm/oom_kill.c

強行翻譯:
為了防止OOM受害者耗盡 在它控制下的 從用戶空間那保留的 內存,我們應該 在授權它訪問保留的內存 之前發(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

image.png

擴展

Spring鉤子方法和鉤子接口的使用

Spring鉤子方法和鉤子接口的使用詳解

參考

Spring優(yōu)雅關閉之:ShutDownHook

理解和配置 Linux 下的 OOM Killer

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容