使用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程序 替代方案:
#!/bin/bashtask_pull_and_submit=`ps -ef | grep -v grep | grep -E 'task_pull_and_submit.py$'| awk '{print $2}'`if (($task_pull_and_submit));thenkill -9 $task_pull_and_submitfiupgrade_system=`ps -ef | grep -v grep | grep -E 'upgrade_system.py$'| awk '{print $2}'`if (($upgrade_system));thenkill -9 $upgrade_systemfi# 先 cd 到绝对目录下执行cd /opt/apps/back_data && nohup python backup_data.py &