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,