I am working on a simple flask Application and I have an endpoint which creates a bash session on request. I want the process to be retained even if I kill flask.
The process is started using subprocess.Popen()
and I want it to persist even after performing killall python
from the terminal.
I tried setting the preexec_fn=os.setsid
and preexec_fn=setpgrp
argument on Popen
but running killall
is still killing the bash-shell that I created within my Flask application.
Relevant code:
def run_command(command):
Process.proc = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
preexec_fn=os.setsid,
)
run_command(['bash', '-c', 'for((i=0;i<1000;i++)); do echo $i; sleep 1; done; echo EOF'])
I'd appreciate any pointers. Thank you.