SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/bin/sh | |
| 2 | ### BEGIN INIT INFO | |
| 3 | # Provides: Octoprint | |
| 4 | # Required-Start: $local_fs networking | |
| 5 | # Required-Stop: | |
| 6 | # Should-Start: | |
| 7 | # Should-Stop: | |
| 8 | # Default-Start: 2 3 4 5 | |
| 9 | # Default-Stop: 0 1 6 | |
| 10 | # Short-Description: Octoprint daemon | |
| 11 | # Description: Starts the Octoprint daemon with the user specified in | |
| 12 | # /etc/default/octoprint. | |
| 13 | ### END INIT INFO | |
| 14 | ||
| 15 | # Author: Sami Olmari | |
| 16 | ||
| 17 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
| 18 | DESC="Octoprint Daemon" | |
| 19 | NAME="Octoprint" | |
| 20 | - | PORT=5000 # On what port to run daemon, default is 5000 |
| 20 | + | |
| 21 | PIDFILE=/var/run/$NAME.pid | |
| 22 | - | DAEMON_ARGS="--port=$PORT" |
| 22 | + | |
| 23 | - | DAEMON_HOME=/home/pi/OctoPrint # Where octoprint is installed |
| 23 | + | |
| 24 | ||
| 25 | - | UMASK=022 # Change this to 0 if running octoprint as its own user |
| 25 | + | |
| 26 | [ -r /etc/default/$PKGNAME ] && . /etc/default/$PKGNAME | |
| 27 | ||
| 28 | # Exit if the octoprint is not installed | |
| 29 | [ -x "$DAEMON_HOME/$DAEMON" ] || exit 0 | |
| 30 | ||
| 31 | # Load the VERBOSE setting and other rcS variables | |
| 32 | [ -f /etc/default/rcS ] && . /etc/default/rcS | |
| 33 | ||
| 34 | # Define LSB log_* functions. | |
| 35 | # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. | |
| 36 | . /lib/lsb/init-functions | |
| 37 | ||
| 38 | if [ -z "$RUN_AT_STARTUP" -o "$RUN_AT_STARTUP" != "YES" ] | |
| 39 | then | |
| 40 | log_warning_msg "Not starting $PKGNAME, edit /etc/default/$PKGNAME to start it." | |
| 41 | exit 0 | |
| 42 | fi | |
| 43 | ||
| 44 | if [ -z "$OCTOPRINT_USER" ] | |
| 45 | then | |
| 46 | log_warning_msg "Not starting $PKGNAME, OCTOPRINT not set in /etc/default/$PKGNAME." | |
| 47 | exit 0 | |
| 48 | fi | |
| 49 | ||
| 50 | # | |
| 51 | # Function to verify if a pid is alive | |
| 52 | # | |
| 53 | is_alive() | |
| 54 | {
| |
| 55 | pid=`cat $1` > /dev/null 2>&1 | |
| 56 | kill -0 $pid > /dev/null 2>&1 | |
| 57 | return $? | |
| 58 | } | |
| 59 | ||
| 60 | # | |
| 61 | # Function that starts the daemon/service | |
| 62 | # | |
| 63 | do_start() | |
| 64 | {
| |
| 65 | # Return | |
| 66 | # 0 if daemon has been started | |
| 67 | # 1 if daemon was already running | |
| 68 | # 2 if daemon could not be started | |
| 69 | ||
| 70 | is_alive $PIDFILE | |
| 71 | RETVAL="$?" | |
| 72 | ||
| 73 | if [ $RETVAL != 0 ]; then | |
| 74 | start-stop-daemon --start --background --quiet --pidfile $PIDFILE --make-pidfile \ | |
| 75 | --exec $DAEMON_HOME/$DAEMON --chuid $OCTOPRINT_USER --user $OCTOPRINT_USER --umask $UMASK -- $DAEMON_ARGS | |
| 76 | RETVAL="$?" | |
| 77 | fi | |
| 78 | } | |
| 79 | ||
| 80 | # | |
| 81 | # Function that stops the daemon/service | |
| 82 | # | |
| 83 | do_stop() | |
| 84 | {
| |
| 85 | # Return | |
| 86 | # 0 if daemon has been stopped | |
| 87 | # 1 if daemon was already stopped | |
| 88 | # 2 if daemon could not be stopped | |
| 89 | # other if a failure occurred | |
| 90 | ||
| 91 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $OCTOPRINT_USER --pidfile $PIDFILE | |
| 92 | RETVAL="$?" | |
| 93 | [ "$RETVAL" = "2" ] && return 2 | |
| 94 | ||
| 95 | rm -f $PIDFILE | |
| 96 | ||
| 97 | [ "$RETVAL" = "0" ] && return 0 || return 1 | |
| 98 | } | |
| 99 | ||
| 100 | case "$1" in | |
| 101 | start) | |
| 102 | [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" | |
| 103 | do_start | |
| 104 | case "$?" in | |
| 105 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; | |
| 106 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; | |
| 107 | esac | |
| 108 | ;; | |
| 109 | stop) | |
| 110 | [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" | |
| 111 | do_stop | |
| 112 | case "$?" in | |
| 113 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; | |
| 114 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; | |
| 115 | esac | |
| 116 | ;; | |
| 117 | restart) | |
| 118 | log_daemon_msg "Restarting $DESC" "$NAME" | |
| 119 | do_stop | |
| 120 | case "$?" in | |
| 121 | 0|1) | |
| 122 | do_start | |
| 123 | case "$?" in | |
| 124 | 0) log_end_msg 0 ;; | |
| 125 | 1) log_end_msg 1 ;; # Old process is still running | |
| 126 | *) log_end_msg 1 ;; # Failed to start | |
| 127 | esac | |
| 128 | ;; | |
| 129 | *) | |
| 130 | # Failed to stop | |
| 131 | log_end_msg 1 | |
| 132 | ;; | |
| 133 | esac | |
| 134 | ;; | |
| 135 | *) | |
| 136 | echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
| |
| 137 | exit 3 | |
| 138 | ;; | |
| 139 | esac | |
| 140 | ||
| 141 | : |