【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 &
相关推荐
0和1的舞者9 分钟前
接口自动化测试详解(二):requests 请求封装与 Pytest 框架全实战
开发语言·自动化测试·python·测试开发·接口·测试
CreasyChan9 分钟前
VirtualBox 安装 CentOS 7.2
linux·运维·centos
Brian Xia18 分钟前
从 0 开始手写 AI Agent 框架:nano-agentscope(二)框架搭建
人工智能·python·ai
写代码的【黑咖啡】28 分钟前
深入了解 Python 中的 Scikit-learn:机器学习的强大工具
python·机器学习·scikit-learn
逆境清醒1 小时前
python教程总目录(更新中ing。。。)
开发语言·python
小北方城市网1 小时前
GEO 智变新篇:质效双升 + 责任共生,打造 AI 时代本地商业长效增长引擎
大数据·人工智能·python·数据库架构
wadesir1 小时前
解决VSCodeServer在CentOS 7上运行问题:glibc 2.17升级与内核优化指南(2025最新版)
linux·运维·centos
Hello_wshuo1 小时前
锅炉温控系统优化
linux·python·物联网
weixin_470740361 小时前
python生成环境部署
开发语言·python
piaopiaolanghua1 小时前
Python中的SGP4轨道预报库
python·sgp4