简体   繁体   中英

Python: executing a long-running process with subprocess.Popen, killing it, and accessing to its output

I'm trying to run a "long-running" process as root (because I have to), in a thread, in python, then kill it, and then access to its output.

The process in question is "babeld", and when I just launch it in a terminal, it outputs text on stdout. Yet, when I run the following code, I do not have any access to stderr or stdout:

% ./example.py                                                                                                                                                                                                                                                                                   
Waiting for output
Pgid: 13445, pid: 13445
Stopping task
Permission Error!
Calling sudo kill 13445
B
None
None
End

The code:

#!/usr/bin/env python3

import subprocess
import threading
import time
import os

def main():
    task = TaskManager()
    task.launch()
    time.sleep(2)
    task.stop()

    print(task.stdout)
    print(task.stderr)


class TaskManager(threading.Thread):
    def __init__(self):
        super().__init__()
        self.start_event = threading.Event()
        self.stderr = None
        self.stdout = None
        self.pgid = None
        self.task = None
        self.start()


    def run(self):
        self.start_event.wait()
        self.task = subprocess.Popen(["sudo", "babeld", "-d", "2", "wlp2s0"],
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE,
                                     preexec_fn=os.setsid)
        self.pgid = os.getpgid(self.task.pid)
        print("Waiting for output")
        self.stdout, self.stderr = self.task.communicate()
        print("End")

    def launch(self):
        self.start_event.set()

    def stop(self):
        print("Pgid: %s, pid: %s" % (self.pgid, self.task.pid))
        try:
            print("Stopping task")
            self.task.terminate()
        except PermissionError:
            print("Permission Error!")
            print("Calling sudo kill %d" % self.pgid)
            subprocess.check_call(["sudo", "kill", str(self.pgid)])
            print("B")

if __name__ == '__main__':
    main()

How to properly kill processes running as root, while having access to their stderr and stdout?

Thanks,

The recipe is simple: do not use communicate . You may replace your self.stdout and self.stderr class attributes with the following getters:

@property
def stdout(self):
    return self.task.stdout.read()

@property
def stderr(self):
    return self.task.stderr.read()

BTW, this approach gives you an opportunity to reject threading usage in your code. Example:

# !/usr/bin/env python3

import subprocess
import time
import os


def main():
    task = TaskManager()
    task.launch()
    time.sleep(2)
    task.stop()

    print(task.stdout)
    print(task.stderr)


class TaskManager:
    def __init__(self):
        self.pgid = None
        self.task = None

    @property
    def stdout(self):
        return self.task.stdout.read()

    @property
    def stderr(self):
        return self.task.stderr.read()

    def launch(self):
        self.task = subprocess.Popen(["sudo", "babeld", "-d", "2", "wlp2s0"],
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE,
                                     preexec_fn=os.setsid)
        self.pgid = os.getpgid(self.task.pid)
        print("Waiting for output")

    def stop(self):
        print("Pgid: %s, pid: %s" % (self.pgid, self.task.pid))
        try:
            print("Stopping task")
            self.task.terminate()
        except PermissionError:
            print("Permission Error!")
            print("Calling sudo kill %d" % self.pgid)
            subprocess.check_call(["sudo", "kill", str(self.pgid)])
            print("B")


if __name__ == '__main__':
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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