pythonprocessdaemon

Start a background process in Python


I'm trying to port a shell script to the much more readable python version. The original shell script starts several processes (utilities, monitors, etc.) in the background with "&". How can I achieve the same effect in python? I'd like these processes not to die when the python scripts complete. I am sure it's related to the concept of a daemon somehow, but I couldn't find how to do this easily.


Solution

  • Note: This answer is less current than it was when posted in 2009. Using the subprocess module shown in other answers is now recommended in the docs

    (Note that the subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using these functions.)


    If you want your process to start in the background you can either use system() and call it in the same way your shell script did, or you can spawn it:

    import os
    os.spawnl(os.P_DETACH, 'some_long_running_command')
    

    (or, alternatively, you may try the less portable os.P_NOWAIT flag).

    See the documentation here.