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