繁体   English   中英

有什么方法可以在进程终止后或终止之前获取proc / pid / status

[英]Is there any way to get proc/pid/status after termination or just before termination of the process

我需要访问proc / pid / status并为一个进程获取VmPeak。对于更长的进程,我可以使用另一个线程来获取它,但是对于小进程,我总是会得到一个错误(没有这样的进程)。是否有任何方法或解决方法我可以用它来完成工作。我使用的是python 2.7,到目前为止,我已经完成了以下工作。

import os
import subprocess
import threading
from shutil import copyfile
import time

rp = None
br = 0
pid = -1


class MemoryThread (threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        global br, rp
        while br == 0:
            try:
                pid = rp.pid
            except Exception as e:
                pid = -1
            if not pid == -1:
                try:
                    pp = open("/proc/" + str(pid) + "/status")
                    st = "X"
                    for l in pp.readlines():
                        if l.startswith("State:"):
                            st = l.split(":", 1)[1].strip().split(' ')[0]
                        if st == "R":
                            copyfile("/proc/" + str(pid) + "/status", "status.txt")
                            print "copied"
                            pp.close()

                except Exception as ee:
                    print ee


th = MemoryThread()
th.start()
cmd = "/home/mursalin/Desktop/A"
cmd = cmd.split(" ")
path_stdout = "/home/mursalin/Desktop/stdout.txt"
path_stderr = "/home/mursalin/Desktop/stderr.txt"
path_stdin = "/home/mursalin/Desktop/inp.txt"
rp = subprocess.Popen(cmd, stdin=open(path_stdin, "r"), stdout=False, stderr=open(
    path_stderr, "w"), shell=False, preexec_fn=False)
rp.communicate()
br = 1

您可能希望子流程保持僵尸状态,您可以在其中收集流程状态。 但是subprocess.call会自动收集僵尸进程,防止子进程处于僵尸状态。

因此,您应该使用像os.spawnos.popen这样的低端api来解决此问题。

暂无
暂无

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

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