Search code examples
pythondaemonpython-2.x

Call a python subprocess as daemon and exit


I'm using a pair of python programs, one of which should call the second.

But this should be done in a way that the first program makes the second one a daemon (or running in the background process), then exits, without waiting for the second program to end.

Is this possible in Python?

I've been looking at os.fork, subprocess module, but I'm quite confused as the correct way to achieve this...


Solution

  • You can use subprocess.Popen for this:

    import subprocess
    
    cmd = ['/usr/bin/python', '/path/to/my/second/pythonscript.py']
    subprocess.Popen(cmd)
    

    You might want to redirect stdout and stderr somewhere, you can do that by passing stdout=<file_obj> to the Popen constructor.