Search code examples
pythonpython-2.7daemon

Python hangs when executing a shell script that runs a process as a daemon


I am trying to use os.system (soon to be replaced with subprocess) to call a shell script (which runs a process as a daemon)

os.system('/path/to/shell_script.sh')

The shell script looks like:

nohup /path/to/program &

If I execute this shell script in my local environment, I have to hit enter before being returned to the console as the shell script is running a process as a daemon. If I do the above command in python, I also have to hit enter before being returned to the console.

However, if I do this in a python program, it just hangs forever.

How can I get the python program to resume execution after calling a shell script that runs a process as a daemon?


Solution

  • From here -

    Within a script, running a command in the background with an ampersand (&) may cause the script to hang until ENTER is hit. This seems to occur with commands that write to stdout.

    You should try redirecting your output to some file (or null if you do not need it), maybe /dev/null , if you really do not need the output.

     nohup /path/to/program > /dev/null &