我试图抓住openvpn pid,然后检查它是否正在运行,但此代码似乎无法正常工作。它告诉我' 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("")
答案 0 :(得分:1)
'432'
不是整数;它是一个包含数字的字符串。使用int()
将其转换为整数。