olmari

/etc/init.d/octoprint

Sep 10th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
  21. DAEMON=run
  22. DAEMON_ARGS="--port=$PORT"
  23. DAEMON_HOME=/home/pi/OctoPrint   # Where octoprint is installed
  24. PIDFILE=/var/run/$NAME.pid
  25. UMASK=022                        # Change this to 0 if running octoprint as its own user
  26. PKGNAME=octoprint
  27. SCRIPTNAME=/etc/init.d/$PKGNAME
  28.  
  29. # Exit if the octoprint is not installed
  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.  
  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.        start-stop-daemon --start --background --quiet --pidfile $PIDFILE --make-pidfile \
  79.        --exec $DAEMON_HOME/$DAEMON --chuid $OCTOPRINT_USER --user $OCTOPRINT_USER --umask $UMASK -- $DAEMON_ARGS
  80.        RETVAL="$?"
  81.    fi
  82. }
  83.  
  84. #
  85. # Function that stops the daemon/service
  86. #
  87. do_stop()
  88. {
  89.    # Return
  90.    #   0 if daemon has been stopped
  91.    #   1 if daemon was already stopped
  92.    #   2 if daemon could not be stopped
  93.    #   other if a failure occurred
  94.  
  95.    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $OCTOPRINT_USER --pidfile $PIDFILE
  96.    RETVAL="$?"
  97.    [ "$RETVAL" = "2" ] && return 2
  98.  
  99.    rm -f $PIDFILE
  100.  
  101.    [ "$RETVAL" = "0"  ] && return 0 || return 1
  102. }
  103.  
  104. case "$1" in
  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.       2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  119.    esac
  120.    ;;
  121.   restart)
  122.    log_daemon_msg "Restarting $DESC" "$NAME"
  123.    do_stop
  124.    case "$?" in
  125.      0|1)
  126.       do_start
  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.       esac
  132.       ;;
  133.      *)
  134.         # Failed to stop
  135.       log_end_msg 1
  136.       ;;
  137.    esac
  138.    ;;
  139.   *)
  140.    echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
  141.    exit 3
  142.    ;;
  143. esac
  144.  
  145. :
Advertisement
Add Comment
Please, Sign In to add comment