start-stop-daemon and python
Asked Answered
C

2

10

I'm trying to start python script with start-stop-daemon:

sudo /sbin/start-stop-daemon --start --pidfile /home/loop.pid \ 
--user www-data --group www-data -b --make-pidfile --chuid www-data \
--exec /usr/bin/python /home/loop.py --verbose

but no python script in my processes. What i do wrong?

loop.py:

import time
while True:
    print "working..."
    time.sleep(3)
Catenane answered 20/12, 2011 at 16:26 Comment(1)
Do you get the pid file?Diploma
V
3

I tried your script and command line, and it is working on my machine. Are you sure your script is located at /home/loop.py?

Also, don't expect to see those prints, because you are specifying the -b (background) option, so the process is being detached from your terminal. Try running it without the -b for testing purposes and then you can redirect the standard output to a logfile with the -stdout option:

sudo /sbin/start-stop-daemon --start --pidfile /home/loop.pid \ 
--user www-data --group www-data -b --make-pidfile --chuid www-data \
--exec /usr/bin/python /home/loop.py --verbose -stdout /var/log/loop.log
Voodooism answered 20/12, 2011 at 16:42 Comment(2)
it start works for me to after reboot, i don't know why) I have ubuntu 11.10 and it seem where is no stdout option in this version of start-stop-daemon. I saw intresting option " -R|--retry <schedule> check whether processes die, and retry." For ex if i kill my script it will start it again? but i can't understand how to use this option in my example?Catenane
Odd, but you're right. Then you'll have to write yourself to stdout from the python script: import sys; sys.stdout.write(STRING).Voodooism
A
0

Rather than exec python directly, if you --exec (or --startas) a nested shell, then you can do the redirection in there (as per this answer):

start-stop-daemon --start --quiet --chuid $DAEMONUSER    \
 --make-pidfile --pidfile $PIDFILE --background       \
 --startas /bin/bash -- -c "exec $DAEMON $DAEMON_ARGS > /var/log/some.log 2>&1"

This works for me and logs my Python stdout quite happily once I realized the output was buffered (my script wasn't writing very much)! I then found this article which uses 'stdbuf' to flush to the output more eagerly than the default (and also explains it quite well):

start-stop-daemon --start --background \
            --pidfile $PIDFILE --make-pidfile --startas /bin/bash \
            -- -c "exec stdbuf -oL -eL $DAEMON $DAEMONARGS > $LOGFILE 2>&1"
Arrogant answered 6/12, 2017 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.