Last active
August 29, 2015 14:22
-
-
Save DaveThw/61b75684ee227a82ef03 to your computer and use it in GitHub Desktop.
init.d script for cloudprint (on a raspberry pi)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### BEGIN INIT INFO | |
# Provides: cloudprint | |
# Required-Start: $local_fs $remote_fs $network | |
# Required-Stop: $local_fs $remote_fs $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start or stop the cloudprint service | |
### END INIT INFO | |
# Can be downloaded and installed in one go by using this command | |
# sudo wget -O /tmp/download https://gist.github.com/DaveThw/61b75684ee227a82ef03/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/cloudprint && sudo update-rc.d cloudprint defaults | |
# Or download and just move to /etc/init.d using this command | |
# sudo wget -O /tmp/download https://gist.github.com/DaveThw/61b75684ee227a82ef03/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/cloudprint | |
# and then start/stop/restart the service with these commands | |
# sudo service cloudprint stop | |
# sudo service cloudprint start | |
# sudo service cloudprint restart | |
# And then, if/when you want the service to autostart on bootup, use this | |
# sudo update-rc.d cloudprint defaults | |
# Conversely, if you want to stop the service autostarting on bootup, use this | |
# sudo update-rc.d -f cloudprint remove | |
# This script is based on my version of the Node-RED init.d script, here: https://gist.github.com/DaveThw/da394c8d04fa2bba55ac | |
# User that launches cloudprint (it's advised to create a new user) | |
# You can do: | |
# sudo useradd cloudprint | |
# then change line below from USER=pi to USER=cloudprint | |
USER=pi | |
# DON'T CHANGE anything below this line unless you know what you're doing! | |
NAME=cloudprint | |
DAEMON="/usr/bin/cloudprint" | |
OPTIONS="" | |
LOG="/var/log/${NAME}.log" | |
PIDFILE="/var/run/${NAME}.pid" | |
. /lib/lsb/init-functions | |
start_daemon () { | |
# If the log file doesn't yet exist, attempt to create it | |
! [ -e "$LOG" ] && sudo touch "$LOG" | |
# Check if USER is the owner of the log file, and chown if not | |
[ -e "$LOG" ] && [ -z "$(find "$LOG" -user $USER)" ] && sudo chown $USER "$LOG" | |
echo >> $LOG | |
date >> $LOG | |
echo "Starting daemon $NAME with command '$DAEMON' and options '$OPTIONS'..." >> $LOG | |
echo >> $LOG | |
start-stop-daemon --start --background \ | |
--chuid $USER \ | |
--name $NAME \ | |
--make-pidfile --pidfile $PIDFILE \ | |
--startas /bin/bash -- -c "exec $DAEMON $OPTIONS >> $LOG 2>&1" | |
# check if the daemon actually started, and return an appropriate result with log_end_msg | |
status="0" | |
pidofproc $DAEMON >/dev/null || status="$?" | |
log_end_msg $status | |
} | |
stop_daemon () { | |
echo >> $LOG | |
date >> $LOG | |
echo "Stopping daemon $NAME..." >> $LOG | |
echo >> $LOG | |
status="0" | |
start-stop-daemon --stop --signal INT --quiet \ | |
--chuid $USER \ | |
--exec $DAEMON --pidfile $PIDFILE --retry 30 \ | |
--oknodo || status="$?" | |
log_end_msg $status | |
} | |
case "$1" in | |
start) | |
log_daemon_msg "Starting daemon" "$NAME" | |
start_daemon | |
;; | |
stop) | |
log_daemon_msg "Stopping daemon" "$NAME" | |
stop_daemon | |
;; | |
restart) | |
log_daemon_msg "Stopping daemon" "$NAME" | |
stop_daemon | |
sleep 5 | |
log_daemon_msg "Re-Starting daemon" "$NAME" | |
start_daemon | |
;; | |
status) | |
status_of_proc "$DAEMON" "$NAME" | |
exit $? | |
;; | |
*) | |
echo "Usage: $0 {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