简体   繁体   English

如何使用python抓取程序PID

[英]How to grab a programs PID using python

i'm trying to grab openvpn pid then check to see if its running but this code doesnt seem to work. 我正在尝试获取openvpn pid,然后检查其是否正在运行,但是此代码似乎不起作用。 it tells me 'pid' isn't an integer when the output is '432' 它告诉我,当输出为“ 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' is not an integer; '432'不是整数; it's a string that holds digits. 这是一个包含数字的字符串。 Convert it to an integer using int() . 使用int()将其转换为整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM