/rc6.d/K02sabnzbdplus

http://github.com/brinkman83/bashrc · Shell · 130 lines · 92 code · 16 blank · 22 comment · 25 complexity · 4f09b67c6398fc6c33665b553f6df638 MD5 · raw file

  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2008-2010 by JCF Ploemen <linux@jp.pp.ru>
  4. # released under GPL, version 2 or later
  5. ################################################
  6. # #
  7. # TO CONFIGURE EDIT /etc/default/sabnzbdplus #
  8. # #
  9. ################################################
  10. ### BEGIN INIT INFO
  11. # Provides: sabnzbdplus
  12. # Required-Start: $local_fs $network $remote_fs
  13. # Required-Stop: $local_fs $network $remote_fs
  14. # Should-Start: NetworkManager
  15. # Should-Stop: NetworkManager
  16. # Default-Start: 2 3 4 5
  17. # Default-Stop: 0 1 6
  18. # Short-Description: SABnzbd+ binary newsgrabber
  19. ### END INIT INFO
  20. DAEMON=/usr/bin/sabnzbdplus
  21. SETTINGS=/etc/default/sabnzbdplus
  22. ([ -x $DAEMON ] && [ -r $SETTINGS ]) || exit 0
  23. DESC="SABnzbd+ binary newsgrabber"
  24. DEFOPTS="--daemon"
  25. PYTHONEXEC="^$(sed -n '1s/^#\!\([a-z0-9\.\/]\+\)\(.*\)/\1(\2)?/p' $DAEMON)"
  26. PIDFILE=/var/run/sabnzbdplus.pid
  27. SETTINGS_LOADED=FALSE
  28. # these are only accepted from the settings file
  29. unset USER CONFIG HOST PORT EXTRAOPTS
  30. . /lib/lsb/init-functions
  31. check_retval() {
  32. if [ $? -eq 0 ]; then
  33. log_end_msg 0
  34. return 0
  35. else
  36. log_end_msg 1
  37. exit 1
  38. fi
  39. }
  40. is_running() {
  41. # returns 0 when running, 1 otherwise
  42. PID="$(pgrep -f -x -u $USER "$PYTHONEXEC $DAEMON $DEFOPTS.*")"
  43. RET=$?
  44. [ $RET -gt 1 ] && exit 1 || return $RET
  45. }
  46. load_settings() {
  47. if [ $SETTINGS_LOADED != "TRUE" ]; then
  48. . $SETTINGS
  49. [ -z "$USER" ] && {
  50. log_warning_msg "$DESC: not configured, aborting. See $SETTINGS";
  51. return 1; }
  52. OPTIONS="$DEFOPTS"
  53. [ -n "$CONFIG" ] && OPTIONS="$OPTIONS --config-file $CONFIG"
  54. [ -n "$HOST" ] && SERVER="$HOST" || SERVER=
  55. [ -n "$PORT" ] && SERVER="$SERVER:$PORT"
  56. [ -n "$SERVER" ] && OPTIONS="$OPTIONS --server $SERVER"
  57. [ -n "$EXTRAOPTS" ] && OPTIONS="$OPTIONS $EXTRAOPTS"
  58. SETTINGS_LOADED=TRUE
  59. fi
  60. return 0
  61. }
  62. start_sab() {
  63. load_settings || exit 0
  64. if ! is_running; then
  65. log_daemon_msg "Starting $DESC"
  66. start-stop-daemon --quiet --chuid $USER --start --exec $DAEMON -- $OPTIONS
  67. check_retval
  68. # create a pidfile; we don't use it but some monitoring app likes to have one
  69. [ -w $(dirname $PIDFILE) ] && \
  70. pgrep -f -x -n -u $USER "$PYTHONEXEC $DAEMON $OPTIONS" > $PIDFILE
  71. else
  72. log_success_msg "$DESC: already running (pid $PID)"
  73. fi
  74. }
  75. stop_sab() {
  76. load_settings || exit 0
  77. if is_running; then
  78. TMPFILE=$(mktemp /tmp/sabnzbdplus.XXXXXXXXXX || exit 1)
  79. trap '[ -f $TMPFILE ] && rm -f $TMPFILE' EXIT
  80. echo "$PID" > $TMPFILE
  81. log_daemon_msg "Stopping $DESC"
  82. start-stop-daemon --stop --user $USER --pidfile $TMPFILE --retry 30
  83. check_retval
  84. else
  85. log_success_msg "$DESC: not running"
  86. fi
  87. [ -f $PIDFILE ] && rm -f $PIDFILE
  88. }
  89. case "$1" in
  90. start)
  91. start_sab
  92. ;;
  93. stop)
  94. stop_sab
  95. ;;
  96. force-reload|restart)
  97. stop_sab
  98. start_sab
  99. ;;
  100. status)
  101. load_settings || exit 4
  102. if is_running; then
  103. log_success_msg "$DESC: running (pid $PID)"
  104. else
  105. log_success_msg "$DESC: not running"
  106. [ -f $PIDFILE ] && exit 1 || exit 3
  107. fi
  108. ;;
  109. *)
  110. log_failure_msg "Usage: $0 {start|stop|restart|force-reload|status}"
  111. exit 3
  112. ;;
  113. esac
  114. exit 0