1

I'm running two command through multprocessing.Process and os.system. One of these commands is adb logcat which produces logs, until SIGTERM or CTRL+C. How to terminate os.system command inside Process?

Sample code:

import time
import os
from multiprocessing import Process

def run_instrument():
    while True:
        time.sleep(3)

def run_logcat():
    logcat_cmd = "adb logcat -s Engine:I"
    os.system(logcat_cmd)

logcat_proc = Process(target=run_logcat)
logcat_proc.start()
inference_proc = Process(target=run_instrument)
inference_proc.start()
inference_proc.join(5)
logcat_proc.terminate()

After call terminate, adb logcat is still present in system via ps aux.

404pio
  • 1,080
  • 1
  • 12
  • 32

1 Answers1

0

After little googling, I have found solution. When I decide to terminate process, I should find its children, and terminate children before. To get children I have used psutil:

import time
import os
import signal
from multiprocessing import Process
import psutil

def run_instrument():
    while True:
        time.sleep(3)

def run_logcat():
    logcat_cmd = "adb logcat -s Engine:I"
    os.system(logcat_cmd)

logcat_proc = Process(target=run_logcat)
logcat_proc.start()
logcat_pid = logcat_proc.pid

inference_proc = Process(target=run_instrument)
inference_proc.start()
inference_proc.join(5)
inference_proc.terminate()

current_process = psutil.Process(logcat_pid)
children = current_process.children(recursive=True)
for child in children:
    os.kill(child.pid, signal.SIGTERM)

logcat_proc.terminate()
Lino
  • 5,084
  • 3
  • 21
  • 39
404pio
  • 1,080
  • 1
  • 12
  • 32
  • I had to add an `if __name__ == '__main__':` wrapper around your code; this is on Python 3.8 but I believe it should not depend on the version. – tripleee Sep 09 '21 at 10:33
  • 1
    An improvement would probably be to replace `os.system()` with `subprocess.run([...])` but you probably still need to find and kill the subprocesses (but now, there should only be one, provided you [avoid `shell=True`.)](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) – tripleee Sep 09 '21 at 10:34
  • 1
    If using `subprocess.Popen` you can put the child in a process group and kill the whole group in just one call without ever needing to look up the PIDs of the individual grandchildren. We have other Q&A describing how. – Charles Duffy Sep 09 '21 at 10:39