Skip to content
Advertisement

“Fire and forget” a process from a Python script

How do I start a process (another Python script, for example) from a Python script so the “child” process is completely detached from the “parent”, so the parent can a) continue on its merry way without waiting for child to finish and b) can be terminated without terminating the child process?

Parent:

JavaScript

Child:

JavaScript

Running parent.py prints:

JavaScript

What I want it to print:

JavaScript

Advertisement

Answer

Answering my own question: I ended up simply using os.system with & at the end of command as suggested by @kevinsa. This allows the parent process to be terminated without the child being terminated.

Here’s some code:

child.py

JavaScript

parent.py, using subprocess.Popen:

JavaScript

Output:

JavaScript
  • note how the child never finishes if the parent is interrupted with Ctrl-C

parent.py, using os.system and &

JavaScript

Output:

JavaScript

Note how the child lives beyond the Ctrl-C.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement