使用python调用shell判断当前进程是否存在
使用subprocess模块判断当前进程是否存在
#! /usr/bin/env python
import subprocess
res = subprocess.Popen(r'ps -ef |grep java |grep -v grep |wc -l',
shell=True,
# 正确值
stdout=subprocess.PIPE,
# 错误值
stderr=subprocess.PIPE,)
# 在windows 中需要使用 decode("gbk")
PID_tomcat_ADP=int(res.stdout.read().decode("utf-8").strip())
if PID_tomcat_ADP != 1:
shell_start = subprocess.Popen(r'/bin/sh /usr/local/tomcat/start.sh',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,)