[英]Execute Linux Command and Get PID
通常我使用:
os.popen("du folder >> 1.txt ").read()
工作正常。
但是,当我想获取子进程ID时,它将返回空值。
os.popen("du folder >> 1.txt &").read() # Notice the & symbol
有谁知道为什么以及如何获取PID?
您将要使用subprocess
模块。
# Can't use shell=True if you want the pid of `du`, not the
# shell, so we have to do the redirection to file ourselves
proc = subprocess.Popen("/usr/bin/du folder", stdout=file("1.txt", "ab"))
print "PID:", proc.pid
print "Return code:", proc.wait()
&
将进程放入后台,并将作业号!= pid。 了解您的过程。
我建议使用子进程-一个Popen
实例具有可以直接访问的属性pid
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.