【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 &
相关推荐
汤姆yu21 分钟前
基于python大数据的小说数据可视化及预测系统
大数据·python·信息可视化
x***J34825 分钟前
Python多线程爬虫
开发语言·爬虫·python
m***D28631 分钟前
Python网络爬虫实战案例
开发语言·爬虫·python
ID_180079054731 小时前
基于 Python 的淘宝商品详情数据结构化解析:SKU、价格与库存字段提取
开发语言·数据结构·python
Laughtin1 小时前
终端Python环境的选择与切换
开发语言·python
JHC0000002 小时前
Python PDF 相关操作
开发语言·python·pdf
databook2 小时前
Manim进阶:用背景图片让你的数学视频脱颖而出
python·动效
温轻舟2 小时前
Python自动办公工具01-Excel文件编辑器
开发语言·python·编辑器·excel·温轻舟
星星上的吴彦祖2 小时前
多模态感知驱动的人机交互决策研究综述
python·深度学习·计算机视觉·人机交互
爱笑的眼睛113 小时前
PyTorch Lightning:重新定义深度学习工程实践
java·人工智能·python·ai