Freddy Freddy - 6 months ago 22
Python Question

Daemontools shell script log to multilog

I have the following shell script:

#!/bin/sh
exec 2>&1
echo "Starting ..."
python /home/services/example.py


The python script consists just of a print command. The above script is a daemontools daemon and also a simple log mechanism is enabled (
multilog
). However, in the
main/log/current
I see the line "Starting ..." but not the printed commands from the python script.

Am I doing something wrong with the call or did I forget something?

EDIT
The python script I am trying to run is:

from time import sleep
import sys

sys.stdout.write("Hello World")
print "Hello World"

while 1:
sys.stdout.write("Hello world")
sleep(10)

Answer

Repeatedly running

sys.stdout.write("Hello world")

...just creates a single huge line of the form Hello worldHello worldHello world, with no guarantee that it ever flushes.

Put newlines on any content you write that doesn't have them implicitly (as through print), and flush your buffers when directly using sys.stdout.write():

while 1:
    sys.stdout.write("Hello world\n")  # add a newline
    sys.stdout.flush()                 # flush the buffer