andrewm@392: #!/bin/sh
andrewm@392: # Script to start the GPIO shutdown button checker
andrewm@392: # Adapted from Stephen C Phillips:
andrewm@392: # http://blog.scphillips.com/posts/2013/07/getting-a-python-script-to-run-in-the-background-as-a-service-on-boot/
andrewm@392: 
andrewm@392: ### BEGIN INIT INFO
andrewm@392: # Provides:          bela_shutdown_swtich
andrewm@392: # Required-Start:    
andrewm@392: # Required-Stop:    
andrewm@392: # Default-Start:     2 3 4 5
andrewm@392: # Default-Stop:      0 1 6
andrewm@392: # Short-Description: Monitor the shutdown button on the Bela cape
andrewm@392: # Description:        Monitor the shutdown button on the Bela cape
andrewm@392: ### END INIT INFO
andrewm@392: 
andrewm@392: # Change the next 3 lines to suit where you install your script and what you want to call it
andrewm@392: DIR=/root
andrewm@392: DAEMON=$DIR/shutdown_switch.sh
andrewm@392: DAEMON_NAME=shutdown_switch
andrewm@392: 
andrewm@392: # Add any command line options for your daemon here
andrewm@392: DAEMON_OPTS=""
andrewm@392: 
andrewm@392: # This next line determines what user the script runs as.
andrewm@392: # Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
andrewm@392: DAEMON_USER=root
andrewm@392: 
andrewm@392: # The process ID of the script when it runs is stored here:
andrewm@392: PIDFILE=/var/run/$DAEMON_NAME.pid
andrewm@392: 
andrewm@392: . /lib/lsb/init-functions
andrewm@392: 
andrewm@392: do_start () {
andrewm@392:     log_daemon_msg "Starting system $DAEMON_NAME daemon"
andrewm@392:     start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
andrewm@392:     log_end_msg $?
andrewm@392: }
andrewm@392: do_stop () {
andrewm@392:     log_daemon_msg "Stopping system $DAEMON_NAME daemon"
andrewm@392:     start-stop-daemon --stop --pidfile $PIDFILE --retry 10
andrewm@392:     log_end_msg $?
andrewm@392: }
andrewm@392: 
andrewm@392: case "$1" in
andrewm@392: 
andrewm@392:     start|stop)
andrewm@392:         do_${1}
andrewm@392:         ;;
andrewm@392: 
andrewm@392:     restart|reload|force-reload)
andrewm@392:         do_stop
andrewm@392:         do_start
andrewm@392:         ;;
andrewm@392: 
andrewm@392:     status)
andrewm@392:         status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
andrewm@392:         ;;
andrewm@392: 
andrewm@392:     *)
andrewm@392:         echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
andrewm@392:         exit 1
andrewm@392:         ;;
andrewm@392: 
andrewm@392: esac
andrewm@392: exit 0