问题
使用python杀死进程后自身程序也会退出,无法执行后边的代码
这样不行:
python
# cmd = " ps -ef | grep -v grep | grep -E 'task_pull_and_submit.py$|upgrade_system.py$'| awk '{print $2}'"
# pids = os.popen(cmd).read().strip('\n').split('\n')
# print(pids)
# for pid in pids:
# os.system("kill -9 {}".format(pid))
解决
使用shell脚本杀死进程,然后再让shell脚本运行该python程序
替代方案:
bash
#!/bin/bash
task_pull_and_submit=`ps -ef | grep -v grep | grep -E 'task_pull_and_submit.py$'| awk '{print $2}'`
if (($task_pull_and_submit));
then
kill -9 $task_pull_and_submit
fi
upgrade_system=`ps -ef | grep -v grep | grep -E 'upgrade_system.py$'| awk '{print $2}'`
if (($upgrade_system));
then
kill -9 $upgrade_system
fi
# 先 cd 到绝对目录下执行
cd /opt/apps/back_data && nohup python backup_data.py &