【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 &
相关推荐
Li emily10 分钟前
如何通过外汇API平台快速实现实时数据接入?
开发语言·python·api·fastapi·美股
m0_5613596717 分钟前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
Ulyanov23 分钟前
顶层设计——单脉冲雷达仿真器的灵魂蓝图
python·算法·pyside·仿真系统·单脉冲
2401_838472511 小时前
使用Python进行图像识别:CNN卷积神经网络实战
jvm·数据库·python
CoLiuRs2 小时前
语义搜索系统原理与实现
redis·python·向量·es
zhihuaba2 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
u0109272712 小时前
Python Web爬虫入门:使用Requests和BeautifulSoup
jvm·数据库·python
Stream_Silver2 小时前
【Agent学习笔记3:使用Python开发简单MCP服务】
笔记·python
穿过锁扣的风2 小时前
零基础入门 Python 爬虫:从基础到实战,爬取虎扑 / 豆瓣 / 图片全掌握
开发语言·爬虫·python
Stream_Silver3 小时前
【Agent学习笔记2:深入理解Function Calling技术:从原理到实践】
笔记·python