0

我正在尝试获取 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("")
4

1 回答 1

1

'432'不是整数;它是一个包含数字的字符串。使用 将其转换为整数int()

于 2016-09-11T19:11:21.470 回答