如何使用 python 获取程序 PID
How to grab a programs PID using python
我正在尝试获取 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("")
'432'
不是整数;它是一个包含数字的字符串。使用 int()
.
将其转换为整数
我正在尝试获取 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("")
'432'
不是整数;它是一个包含数字的字符串。使用 int()
.