I have a Python application that starts a subprocess that runs a bash script in another thread and in a separate shell. Note that this script starts a REST service and thus it does not terminate until its explicitly told to (e.g. using crl+c).
I would like to be able to kill the subprocess as well. However, self.subprocess.kill()
does not seem to do anything at all. I’ve tried self.subprocess.terminate()
as well. However, the script continues to run as normal.
from subprocess import Popen
import threading
class ProcessManager:
def __init__(self):
self.process = None
def run_process(self):
self.process = Popen("start; sh script.sh", shell=True)
def kill_process(self):
self.process.kill()
process_manager = ProcessManager()
thread = threading.Thread(target=process_manager.run_process, args=[])
thread.start()
process_manager.kill_process()
I’m not sure if this has anything to do with the fact that I have start;
in my command. This was the only way I could force the bash script to be executed in a separate shell than the one that’s running the Python application. Using shell=True
on its own did not do the trick.
Lastly, the reason for calling run_process
in a separate thread is because this method is blocking. As my Python application continues to be built, I will add another similar method runn_process2
that executes another bash script concurrently.