Stories

Detail Return Return

Linux 下 kill 孫子進程(qbit) - Stories Detail

  • qbit 在使用 Python 的 FastAPI 框架,在 Python 腳本中用 os.system 調用了 uvicorn 啓動程序,調用鏈條大致如下
python3 main.py             # 進程 1
  -- sh python3 uvicorn     # 進程 2
      -- python3 uvicorn    # 進程 3
  • qbit 發現直接 kill 進程 1 或 進程 2,並不能終止進程 3。即 kill 某個進程,並不一定能終止該進程的子進程和孫子進程。
  • 於是通過找到孫子進程的方式來終止進程
  • 編寫重啓腳本如下
echo "重啓 run_qbit 進程"
echo "獲取進程 ID"
pid=`pgrep -f run_qbit`
# 殺掉孫子進程
if [ -z "$pid" ]; then
    echo "未找到需 kill 的 run_qbit 進程"
else
    echo "找到進程:"$pid", 即將 kill ..."
    kill `pgrep -P $(pgrep -P $pid)`
fi
echo "即將啓動 run_qbit 進程 ..."
export CfgFile=/home/qbit/config.toml
nohup python /home/qbit/app/main.py run_qbit > /dev/null 2>&1 &
  • 腳本中的 run_qbit 是為了唯一定位入口進程,跟 Python 程序本身無關
qbit snap
user avatar coulthard Avatar linong Avatar zlibrarynew Avatar changhao_flag Avatar yinuo112 Avatar moluo_5eaed351f2b16 Avatar jdcdevloper Avatar
Favorites 7 users favorite the story!
Favorites

Add a new Comments

Some HTML is okay.