【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 &
相关推荐
Roselind_Yi6 分钟前
从线性回归实战到Python依赖安装踩坑:我的机器学习入门排雷记
笔记·python·算法·机器学习·回归·线性回归·学习方法
干啥啥不行,秃头第一名16 分钟前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
二闹17 分钟前
一文了解Python里的函数名和函数参数到底啥关系?
后端·python
sin°θ_陈31 分钟前
前馈式3D Gaussian Splatting 研究地图(路线一):像素对齐高斯的起点——pixelSplat 与 latentSplat 在解决什么
python·深度学习·3d·aigc·webgl·3dgs·空间智能
Bert.Cai38 分钟前
Python标识符详解
开发语言·python
Allen_LVyingbo38 分钟前
自进化医疗智能体:动态记忆与持续运行的Python架构编程(上)
数据结构·python·架构·动态规划·健康医疗
lifewange39 分钟前
insert
开发语言·python
代码探秘者43 分钟前
【算法篇】5.链表
java·数据结构·人工智能·python·算法·spring·链表
杰杰7981 小时前
一文掌握在Flask使用SQLAlchemy(上)
后端·python·flask