|
| 1 | +#! /bin/sh |
| 2 | +### BEGIN INIT INFO |
| 3 | +# Provides: ospi_gc |
| 4 | +# Required-Start: $remote_fs $syslog |
| 5 | +# Required-Stop: $remote_fs $syslog |
| 6 | +# Default-Start: 2 3 4 5 |
| 7 | +# Default-Stop: 0 1 6 |
| 8 | +# Short-Description: OpenSprinkler + Raspberry Pi + GoogleCalendar |
| 9 | +# Description: OpenSprinkler + Raspberry Pi + GoogleCalendar - |
| 10 | +# Raspberry Pi with OpenSprinkler Pi board from Ray's Hobby |
| 11 | +# and google calendar integratoin |
| 12 | +### END INIT INFO |
| 13 | + |
| 14 | +# |
| 15 | +# To auto start on boot execute (once) as root |
| 16 | +# |
| 17 | +# update-rc.d ospi_gc defaults |
| 18 | +# |
| 19 | +# To stop auto start on boot execute |
| 20 | +# |
| 21 | +# update-rc.d ospi_gc remove |
| 22 | +# |
| 23 | + |
| 24 | +# Author: Jason Mechler <github at jasonmechler.fastmail.fm> |
| 25 | +# |
| 26 | +# Please remove the "Author" lines above and replace them |
| 27 | +# with your own name if you copy and modify this script. |
| 28 | + |
| 29 | +# Do NOT "set -e" |
| 30 | + |
| 31 | +# PATH should only include /usr/* if it runs after the mountnfs.sh script |
| 32 | +PATH=/sbin:/usr/sbin:/bin:/usr/bin |
| 33 | +DESC="OpenSprinkler Raspberry Pi Google Calendar" |
| 34 | +NAME=ospi_gc |
| 35 | +DAEMON=/usr/bin/python |
| 36 | +DAEMON_ARGS="ospi_gc.py" |
| 37 | +HOMEDIR=/home/pi/demo/gcalendar # Edit if different on your Raspberry Pi |
| 38 | +PIDFILE=/var/run/$NAME.pid |
| 39 | +SCRIPTNAME=/etc/init.d/$NAME |
| 40 | + |
| 41 | +# Exit if the package is not installed |
| 42 | +[ -x "$DAEMON" ] || exit 0 |
| 43 | + |
| 44 | +# Read configuration variable file if it is present |
| 45 | +[ -r /etc/default/$NAME ] && . /etc/default/$NAME |
| 46 | + |
| 47 | +# Load the VERBOSE setting and other rcS variables |
| 48 | +. /lib/init/vars.sh |
| 49 | + |
| 50 | +# Define LSB log_* functions. |
| 51 | +# Depend on lsb-base (>= 3.2-14) to ensure that this file is present |
| 52 | +# and status_of_proc is working. |
| 53 | +. /lib/lsb/init-functions |
| 54 | + |
| 55 | +# |
| 56 | +# Function that starts the daemon/service |
| 57 | +# |
| 58 | +do_start() |
| 59 | +{ |
| 60 | + # Return |
| 61 | + # 0 if daemon has been started |
| 62 | + # 1 if daemon was already running |
| 63 | + # 2 if daemon could not be started |
| 64 | + start-stop-daemon --start --quiet --chdir $HOMEDIR --pidfile $PIDFILE --make-pidfile --background --exec $DAEMON --test > /dev/null \ |
| 65 | + || return 1 |
| 66 | + start-stop-daemon --start --quiet --chdir $HOMEDIR --pidfile $PIDFILE --make-pidfile --background --exec $DAEMON -- \ |
| 67 | + $DAEMON_ARGS \ |
| 68 | + || return 2 |
| 69 | + # Add code here, if necessary, that waits for the process to be ready |
| 70 | + # to handle requests from services started subsequently which depend |
| 71 | + # on this one. As a last resort, sleep for some time. |
| 72 | +} |
| 73 | + |
| 74 | +# |
| 75 | +# Function that stops the daemon/service |
| 76 | +# |
| 77 | +do_stop() |
| 78 | +{ |
| 79 | + # Return |
| 80 | + # 0 if daemon has been stopped |
| 81 | + # 1 if daemon was already stopped |
| 82 | + # 2 if daemon could not be stopped |
| 83 | + # other if a failure occurred |
| 84 | + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE |
| 85 | + RETVAL="$?" |
| 86 | + [ "$RETVAL" = 2 ] && return 2 |
| 87 | + # Wait for children to finish too if this is a daemon that forks |
| 88 | + # and if the daemon is only ever run from this initscript. |
| 89 | + # If the above conditions are not satisfied then add some other code |
| 90 | + # that waits for the process to drop all resources that could be |
| 91 | + # needed by services started subsequently. A last resort is to |
| 92 | + # sleep for some time. |
| 93 | + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON |
| 94 | + [ "$?" = 2 ] && return 2 |
| 95 | + # Many daemons don't delete their pidfiles when they exit. |
| 96 | + rm -f $PIDFILE |
| 97 | + return "$RETVAL" |
| 98 | +} |
| 99 | + |
| 100 | +# |
| 101 | +# Function that sends a SIGHUP to the daemon/service |
| 102 | +# |
| 103 | +do_reload() { |
| 104 | + # |
| 105 | + # If the daemon can reload its configuration without |
| 106 | + # restarting (for example, when it is sent a SIGHUP), |
| 107 | + # then implement that here. |
| 108 | + # |
| 109 | + start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME |
| 110 | + return 0 |
| 111 | +} |
| 112 | + |
| 113 | +case "$1" in |
| 114 | + start) |
| 115 | + log_daemon_msg "Starting $DESC" "$NAME" |
| 116 | + do_start |
| 117 | + case "$?" in |
| 118 | + 0|1) log_end_msg 0 ;; |
| 119 | + 2) log_end_msg 1 ;; |
| 120 | + esac |
| 121 | + ;; |
| 122 | + stop) |
| 123 | + log_daemon_msg "Stopping $DESC" "$NAME" |
| 124 | + do_stop |
| 125 | + case "$?" in |
| 126 | + 0|1) log_end_msg 0 ;; |
| 127 | + 2) log_end_msg 1 ;; |
| 128 | + esac |
| 129 | + ;; |
| 130 | + status) |
| 131 | + status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? |
| 132 | + ;; |
| 133 | + #reload|force-reload) |
| 134 | + # |
| 135 | + # If do_reload() is not implemented then leave this commented out |
| 136 | + # and leave 'force-reload' as an alias for 'restart'. |
| 137 | + # |
| 138 | + #log_daemon_msg "Reloading $DESC" "$NAME" |
| 139 | + #do_reload |
| 140 | + #log_end_msg $? |
| 141 | + #;; |
| 142 | + restart|force-reload) |
| 143 | + # |
| 144 | + # If the "reload" option is implemented then remove the |
| 145 | + # 'force-reload' alias |
| 146 | + # |
| 147 | + log_daemon_msg "Restarting $DESC" "$NAME" |
| 148 | + do_stop |
| 149 | + case "$?" in |
| 150 | + 0|1) |
| 151 | + do_start |
| 152 | + case "$?" in |
| 153 | + 0) log_end_msg 0 ;; |
| 154 | + 1) log_end_msg 1 ;; # Old process is still running |
| 155 | + *) log_end_msg 1 ;; # Failed to start |
| 156 | + esac |
| 157 | + ;; |
| 158 | + *) |
| 159 | + # Failed to stop |
| 160 | + log_end_msg 1 |
| 161 | + ;; |
| 162 | + esac |
| 163 | + ;; |
| 164 | + *) |
| 165 | + #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 |
| 166 | + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 |
| 167 | + exit 3 |
| 168 | + ;; |
| 169 | +esac |
| 170 | + |
| 171 | +: |
0 commit comments