Skip to content

Instantly share code, notes, and snippets.

@andrewn
Created August 21, 2013 16:23
Show Gist options
  • Save andrewn/6296697 to your computer and use it in GitHub Desktop.
Save andrewn/6296697 to your computer and use it in GitHub Desktop.
Starting services as daemons on Raspbian
  • Save the script as /etc/init.d/raspi-printer
  • Make it executable chmod +x /etc/init.d/raspi-printer
  • Start by doing service raspi-printer start

To run automatically, you run the update-rc.d script:

sudo update-rc.d -f raspi-printer defaults

See: http://jimmyg.org/blog/2010/python-daemon-init-script.html

#! /bin/sh
# /etc/init.d/raspi-printer
### BEGIN INIT INFO
# Provides: raspi-printer
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Enable raspi-printer service.
# Description: Enable raspi-printer service.
### END INIT INFO
# See: http://jimmyg.org/blog/2010/python-daemon-init-script.html
#set -e
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
DIR="/home/pi/raspi-printer"
DAEMON="/home/pi/raspi-printer/printer"
ARGS=""
PIDFILE="/var/run/raspi-printer.pid"
. /lib/init/vars.sh
# Used by status command
# See: http://archives.aidanfindlater.com/blog/2009/09/04/sample-init-d-script/
. /lib/lsb/init-functions
echo "$(date) $(whoami) raspi-printer script" >> /tmp/printer.debug
case "$1" in
start)
if /sbin/start-stop-daemon --status --pidfile $PIDFILE
then echo "Already running"; exit 0; fi
echo "$(date) raspi-printer starting server: $PIDFILE $DIR $DAEMON $ARGS" >> /tmp/printer.debug
echo "Starting server"
/sbin/start-stop-daemon --start --pidfile $PIDFILE \
--background --make-pidfile --chdir $DIR \
--exec $DAEMON $ARGS
echo "$(date) raspi-printer start-stop-daemon started" >> /tmp/printer.debug
;;
stop)
echo "Stopping server"
echo "raspi-printer stopping server" >> /tmp/printer.debug
/sbin/start-stop-daemon --stop --pidfile $PIDFILE --verbose --signal INT
rm $PIDFILE
;;
restart)
$0 stop
$0 start
;;
status)
status_of_proc -p $PIDFILE $DAEMON printer && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/raspi-printer {start|stop|restart|status}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment