【python】【centos】使用python杀死进程后自身也会退出

问题

使用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 &
相关推荐
花酒锄作田6 小时前
Postgres - Listen/Notify构建轻量级发布订阅系统
python·postgresql
Thomas.Sir6 小时前
第二章:LlamaIndex 的基本概念
人工智能·python·ai·llama·llamaindex
m0_694845577 小时前
Dify部署教程:从AI原型到生产系统的一站式方案
服务器·人工智能·python·数据分析·开源
李昊哲小课8 小时前
Python办公自动化教程 - 第7章 综合实战案例 - 企业销售管理系统
开发语言·python·数据分析·excel·数据可视化·openpyxl
不知名的老吴8 小时前
返回None还是空集合?防御式编程的关键细节
开发语言·python
李昊哲小课9 小时前
Python办公自动化教程 - 第5章 图表创建 - 让数据可视化
python·信息可视化·数据分析·数据可视化·openpyxl
chushiyunen9 小时前
python pygame实现贪食蛇
开发语言·python·pygame
Dream of maid9 小时前
Python-基础2(流程控制)
python
Lenyiin10 小时前
《Python 修炼全景指南:一》从环境搭建到第一个程序
开发语言·python
涛声依旧3931610 小时前
Python项目实战:学生信息管理系统
开发语言·python·数据挖掘