Bootstrap

linux应用

检查Python程序未运行则重新运行

entity_program定时杀掉进程重新运行
match_program定时检查是否运行,未运行则启动
(注意echo时间时,date和+中间要有空格)

#!/bin/bash
  
# 检测的Python程序名称
entity_program="entity.py"
match_program="match.py"

# 获取Python进程的PID
PID=$(pgrep -f $entity_program)
if [[ -n $PID ]]
then
  echo $(date +"%Y-%m-%d %H:%M:%S") "Python程序 $entity_program 在运行中.正在kill"
  kill $PID
  echo "killed, starting ..."
  # cd /home/xxx/match/service
  # /opt/conda/envs/match/bin/python3.7 entity.py >/dev/null 2>&1 &
  echo "已启动"
else
  echo $(date +"%Y-%m-%d %H:%M:%S") "Python程序 $entity_program 没有在运行,正在重启..."
  # 重启Python程序的命令,例如:
  # cd /home/xxx/match/service
  # /opt/conda/envs/match/bin/python3.7 entity.py >/dev/null 2>&1 &
  echo "已启动"
fi

if pgrep -f "$match_program" >/dev/null
then
  echo $(date +"%Y-%m-%d %H:%M:%S") "Python程序 $match_program 在运行中."
else
  echo $(date +"%Y-%m-%d %H:%M:%S") "Python程序 $match_program 没有在运行,正在重启..."
  # 重启Python程序的命令,例如:
  # cd /home/xxx/match/service
  # /opt/conda/envs/match/bin/python3.7 match.py >/dev/null 2>&1 &
  echo "已启动"
fi
50 11 * * * /home/xxx/check/run.sh >> /home/xxx/check/logfile.log 2>&1
30 0 * * * /bin/bash /data/dgcy_code/dist/algorithm_check.sh >> /data/dgcy_code/dist/algorithm_check.log 2>&1 &
# 完整路径数组
programs=("/data/dgcy_code/dist/python_new")

# 循环读取数据中的文件名
for pg in "${programs[@]}"
do
    # 获取文件名
    filename=$(basename "$pg")
    pathname=$(dirname "$pg")
    PID=$(pgrep -f $filename)
    if [[ -n $PID ]]
    then
      echo $(date +"%Y-%m-%d %H:%M:%S") "Python程序$filename在运行中.正在kill"
      kill $PID
      echo "killed, starting ..."
      cd $pathname
      nohup ./$filename >/dev/null 2>&1 &
      echo "已启动"
    else
      echo $(date +"%Y-%m-%d %H:%M:%S") "Python程序$filename没有在运行,正在重启..."
      # 重启Python程序的命令,例如:
      cd $pathname
      nohup ./$filename >/dev/null 2>&1 &
      echo "已启动"
    fi
done
;