我正在尝试获取 openvpn pid,然后检查它是否正在运行,但这段代码似乎不起作用。当输出为“432”时,它告诉我“pid”不是整数
import psutil
import time
import os
import subprocess
proc = subprocess.Popen(["pgrep openvpn"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
out = out.strip()
print ("openvpn",out)
pid = out
time.sleep(5)
while True:
if psutil.pid_exists(pid):
print "a process with pid %d exists" % pid
time.sleep(120)
else:
print "a process with pid %d does not exist" % pid
time.sleep(5)
os.system("")