pythonlinuxpython-3.xdaemonstart-stop-daemon

Can't read stdout output of external commands after deamonizing Python script


I wrote a RPC server foo in Python that I deamonized with

start-stop-daemon --start --quiet --background \
                  --make-pidfile \
                  --pidfile /var/run/foo.pid \
                  -- /opt/foo

Everything works fine except that every use of subprocess.check_call(some_cmd) or os.popen(some_cmd).read(1048576) in my daemon returns an empty string compared to the case when I start foo in the foreground.

The external program some_cmd is still executed sucessfully (as I can see in the logs), yet I don't have access to the stuff it prints to stdout.

Why is that and what changes can I make to either the start-stop-daemon call or my Python code in ordert to fix this?

PS: I found that only the output of certain commands is now empty: subprocess.check_output('echo "Hello World"', shell=True) still behaves normally, whereas the output of subprocess.check_output('/etc/init.d/apache2 --nocolor reload', shell=True) is now empty.


Solution

  • I solved it myself. The problem was that start-stop-daemon is setting the environment variable EINFO_QUIET when run with the --quite flag (not documented in the man pages...).

    OpenRC init scripts then prints nothing to stdout when this variable is set. A simple

    import os
    os.environ["EINFO_QUIET"] = "NO"
    

    solves the problem.