3

I have a python script that uses Flask to run a web server on my Raspberry Pi. This script is working just fine when run from the command line directly but I would love for it to start in the background and be able to be restarted, stopped, and started like a normal daemon.

I followed the tutorial here: Python in background process which helped out but I still have some issues. The daemon starts just fine on boot but I can't control it very well. When I try to stop the dameon, it doesn't actually stop it. The python script keeps on running. Also, I can keep starting the dameon without any errors but the "status" command always says it is not running. The restart sometimes will actually kill the original script and then start it again but the stop command never actually stops it.

My init.d script is:

    #!/bin/sh

    ### BEGIN INIT INFO
    # Provides: myscript
    # Required-Start: $remote_fs $syslog
    # Required-Stop: $remote_fs $syslog
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Media remote for Pi
    # Description: Interact with program via phone
    ### END INIT INFO

    # Change the next 3 lines to suit where you install your script and what you want to call it
    DIR=/usr/local/bin/MyPythonScript
    DAEMON=$DIR/myscript.py
    DAEMON_NAME=myscript

    # This next line determines what user the script runs as.
    # Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
    DAEMON_USER=pi

    # The process ID of the script when it runs is stored here:
    PIDFILE=/var/run/$DAEMON_NAME.pid

    . /lib/lsb/init-functions

    do_start () {
            log_daemon_msg "Starting system $DAEMON_NAME daemon"
            start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --startas $DAEMON
            log_end_msg $?
    }
    do_stop () {
            log_daemon_msg "Stopping system $DAEMON_NAME daemon"
            start-stop-daemon --stop --pidfile $PIDFILE --retry 10
            log_end_msg $?
    }

    case "$1" in
            start|stop)
                    do_${1}
                    ;;

            restart|reload|force-reload)
                    do_stop
                    do_start
                    ;;

            status)
                    status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
                    ;;
            *)

            echo "Usage: /etc/init.d/$DEAMON_NAME {start|stop|restart|status}"
            exit 1
            ;;
    esac
    exit

If I run the script from the command line, it works perfectly. I just want to be able to start, stop, restart, and give "running / not running" from a daemon but I can't quite figure it out.

1
  • I made a custom daemon using upstart service to run and kill it. I was having problems related with the TERM signal handler. I made it in Ubuntu, but the service works similarly, can you stop the raspberry normally when the service is running? In my case the service couldn't kill the daemon and the laptop with Ubuntu couldn't shutdown.
    – ssoto
    Dec 30, 2013 at 10:55

1 Answer 1

2

I am doing the same thing. For daemon management, I am using supervisord. It is a better solution than creating your own daemon.

For more information see,

http://supervisord.org/index.html

https://www.digitalocean.com/community/articles/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.