| 1 | = Ubuntu Init Script = |
| 2 | |
| 3 | Based upon the script created [http://apocryph.org/2008/11/30/setting_deluge_headless_ubuntu_seedbox_windows_client/ here]. |
| 4 | |
| 5 | Put this in /etc/default/deluge-daemon and set DELUGED_USER to the user you which to run deluge as |
| 6 | |
| 7 | {{{ |
| 8 | #!sh |
| 9 | # Configuration for /etc/init.d/deluge-daemon |
| 10 | |
| 11 | # The init.d script will only run if this variable non-empty. |
| 12 | DELUGED_USER="" # !!!CHANGE THIS!!!! |
| 13 | |
| 14 | # Should we run at startup? |
| 15 | RUN_AT_STARTUP="YES" |
| 16 | }}} |
| 17 | |
| 18 | This should be placed in /etc/init.d/deluge-daemon |
| 19 | {{{ |
| 20 | #!sh |
| 21 | #!/bin/sh |
| 22 | ### BEGIN INIT INFO |
| 23 | # Provides: deluge-daemon |
| 24 | # Required-Start: $local_fs $remote_fs |
| 25 | # Required-Stop: $local_fs $remote_fs |
| 26 | # Should-Start: $network |
| 27 | # Should-Stop: $network |
| 28 | # Default-Start: 2 3 4 5 |
| 29 | # Default-Stop: 0 1 6 |
| 30 | # Short-Description: Daemonized version of deluge and webui. |
| 31 | # Description: Starts the deluge daemon with the user specified in |
| 32 | # /etc/default/deluge-daemon. |
| 33 | ### END INIT INFO |
| 34 | |
| 35 | # Author: Adolfo R. Brandes |
| 36 | |
| 37 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
| 38 | DESC="Deluge Daemon" |
| 39 | NAME1="deluged" |
| 40 | NAME2="deluge" |
| 41 | DAEMON1=/usr/bin/deluged |
| 42 | DAEMON1_ARGS="-d" # Consult `man deluged` for more options |
| 43 | DAEMON2=/usr/bin/deluge-web |
| 44 | DAEMON2_ARGS="" # Consult `man deluge-web` for more options |
| 45 | PIDFILE1=/var/run/$NAME1.pid |
| 46 | PIDFILE2=/var/run/$NAME2.pid |
| 47 | UMASK=22 # Change this to 0 if running deluged as its own user |
| 48 | PKGNAME=deluge-daemon |
| 49 | SCRIPTNAME=/etc/init.d/$PKGNAME |
| 50 | |
| 51 | # Exit if the package is not installed |
| 52 | [ -x "$DAEMON1" -a -x "$DAEMON2" ] || exit 0 |
| 53 | |
| 54 | # Read configuration variable file if it is present |
| 55 | [ -r /etc/default/$PKGNAME ] && . /etc/default/$PKGNAME |
| 56 | |
| 57 | # Load the VERBOSE setting and other rcS variables |
| 58 | [ -f /etc/default/rcS ] && . /etc/default/rcS |
| 59 | |
| 60 | # Define LSB log_* functions. |
| 61 | # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. |
| 62 | . /lib/lsb/init-functions |
| 63 | |
| 64 | if [ -z "$RUN_AT_STARTUP" -o "$RUN_AT_STARTUP" != "YES" ] |
| 65 | then |
| 66 | log_warning_msg "Not starting $PKGNAME, edit /etc/default/$PKGNAME to start it." |
| 67 | exit 0 |
| 68 | fi |
| 69 | |
| 70 | if [ -z "$DELUGED_USER" ] |
| 71 | then |
| 72 | log_warning_msg "Not starting $PKGNAME, DELUGED_USER not set in /etc/default/$PKGNAME." |
| 73 | exit 0 |
| 74 | fi |
| 75 | |
| 76 | # |
| 77 | # Function that starts the daemon/service |
| 78 | # |
| 79 | do_start() |
| 80 | { |
| 81 | # Return |
| 82 | # 0 if daemon has been started |
| 83 | # 1 if daemon was already running |
| 84 | # 2 if daemon could not be started |
| 85 | start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --exec $DAEMON1 \ |
| 86 | --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK --test > /dev/null |
| 87 | RETVAL1="$?" |
| 88 | start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --exec $DAEMON2 \ |
| 89 | --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK --test > /dev/null |
| 90 | RETVAL2="$?" |
| 91 | [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 1 |
| 92 | |
| 93 | start-stop-daemon --start --background --quiet --pidfile $PIDFILE1 --make-pidfile --exec $DAEMON1 \ |
| 94 | --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON1_ARGS |
| 95 | RETVAL1="$?" |
| 96 | sleep 2 |
| 97 | start-stop-daemon --start --background --quiet --pidfile $PIDFILE2 --make-pidfile --exec $DAEMON2 \ |
| 98 | --chuid $DELUGED_USER --user $DELUGED_USER --umask $UMASK -- $DAEMON2_ARGS |
| 99 | RETVAL2="$?" |
| 100 | [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] || return 2 |
| 101 | } |
| 102 | |
| 103 | # |
| 104 | # Function that stops the daemon/service |
| 105 | # |
| 106 | do_stop() |
| 107 | { |
| 108 | # Return |
| 109 | # 0 if daemon has been stopped |
| 110 | # 1 if daemon was already stopped |
| 111 | # 2 if daemon could not be stopped |
| 112 | # other if a failure occurred |
| 113 | |
| 114 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE2 |
| 115 | RETVAL2="$?" |
| 116 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $DELUGED_USER --pidfile $PIDFILE1 |
| 117 | RETVAL1="$?" |
| 118 | [ "$RETVAL1" = "2" -o "$RETVAL2" = "2" ] && return 2 |
| 119 | |
| 120 | rm -f $PIDFILE1 $PIDFILE2 |
| 121 | |
| 122 | [ "$RETVAL1" = "0" -a "$RETVAL2" = "0" ] && return 0 || return 1 |
| 123 | } |
| 124 | |
| 125 | case "$1" in |
| 126 | start) |
| 127 | [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME1" |
| 128 | do_start |
| 129 | case "$?" in |
| 130 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; |
| 131 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; |
| 132 | esac |
| 133 | ;; |
| 134 | stop) |
| 135 | [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME1" |
| 136 | do_stop |
| 137 | case "$?" in |
| 138 | 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; |
| 139 | 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; |
| 140 | esac |
| 141 | ;; |
| 142 | restart|force-reload) |
| 143 | log_daemon_msg "Restarting $DESC" "$NAME1" |
| 144 | do_stop |
| 145 | case "$?" in |
| 146 | 0|1) |
| 147 | do_start |
| 148 | case "$?" in |
| 149 | 0) log_end_msg 0 ;; |
| 150 | 1) log_end_msg 1 ;; # Old process is still running |
| 151 | *) log_end_msg 1 ;; # Failed to start |
| 152 | esac |
| 153 | ;; |
| 154 | *) |
| 155 | # Failed to stop |
| 156 | log_end_msg 1 |
| 157 | ;; |
| 158 | esac |
| 159 | ;; |
| 160 | *) |
| 161 | echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 |
| 162 | exit 3 |
| 163 | ;; |
| 164 | esac |
| 165 | |
| 166 | : |
| 167 | |
| 168 | }}} |
| 169 | |
| 170 | Make the script executable by root |
| 171 | |
| 172 | {{{ |
| 173 | #!sh |
| 174 | sudo chmod 755 /etc/init.d/deluge-daemon |
| 175 | }}} |
| 176 | |
| 177 | Run this script on start up |
| 178 | |
| 179 | {{{ |
| 180 | #!sh |
| 181 | sudo update-rc.d deluge-daemon defaults |
| 182 | }}} |
| 183 | |
| 184 | Start the daemon |
| 185 | |
| 186 | {{{ |
| 187 | #!sh |
| 188 | sudo /etc/init.d/deluge-daemon start |
| 189 | }}} |