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.

avi
5133 gold badges7 silver badges14 bronze badges
asked Dec 29, 2013 at 16:46
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. Commented 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

answered Apr 29, 2014 at 19:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.