【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 &
相关推荐
cnxy1887 小时前
围棋对弈Python程序开发完整指南:步骤4 - 提子逻辑和劫争规则实现
开发语言·python·机器学习
TheSumSt7 小时前
Python丨课程笔记Part3:语法进阶部分(控制结构与基础数据结构)
数据结构·笔记·python
ha_lydms8 小时前
5、Spark函数_s/t
java·大数据·python·spark·数据处理·maxcompute·spark 函数
电商API&Tina8 小时前
跨境电商 API 对接指南:亚马逊 + 速卖通接口调用全流程
大数据·服务器·数据库·python·算法·json·图搜索算法
Yyyyy123jsjs8 小时前
外汇Tick数据交易时段详解与Python实战分析
人工智能·python·区块链
默默前行的虫虫9 小时前
nicegui地图总结
网络·python
不易思不逸10 小时前
SAM2 测试
人工智能·python
趣知岛11 小时前
智能家居与物联网项目实战全指南:从架构设计到落地部署
python·物联网·智能家居
龘龍龙11 小时前
Python基础(八)
开发语言·python
vibag12 小时前
FastAPI框架
python·pycharm·fastapi