| 1 | = init.d Script = |
| 2 | == Notes == |
| 3 | This should be installed at {{{/etc/init.d/}}} as {{{transmission-daemon}}}. |
| 4 | == Script == |
| 5 | {{{ |
| 6 | #! /bin/sh |
| 7 | ### BEGIN INIT INFO |
| 8 | # Provides: transmission-daemon |
| 9 | # Required-Start: networking |
| 10 | # Required-Stop: networking |
| 11 | # Default-Start: 2 3 5 |
| 12 | # Default-Stop: 0 1 6 |
| 13 | # Short-Description: Start the transmission BitTorrent daemon client. |
| 14 | ### END INIT INFO |
| 15 | |
| 16 | # Do NOT "set -e" |
| 17 | |
| 18 | # |
| 19 | # ----- CONFIGURATION ----- |
| 20 | # For the default location Transmission uses, visit: |
| 21 | # http://trac.transmissionbt.com/wiki/ConfigFiles |
| 22 | # For a guide on how set the preferences, visit: |
| 23 | # http://trac.transmissionbt.com/wiki/EditConfigFiles |
| 24 | # For the available environement variables, visit: |
| 25 | # http://trac.transmissionbt.com/wiki/EnvironmentVariables |
| 26 | |
| 27 | |
| 28 | # The folder where Transmission stores the config |
| 29 | # Only change this you have it at a non-default location |
| 30 | #TRANSMISSION_HOME="etc/.config/transmission-daemon" |
| 31 | #TRANSMISSION_WEB_HOME="/usr/local/share/" |
| 32 | |
| 33 | # The name of the user that should run Transmission |
| 34 | USERNAME=root |
| 35 | |
| 36 | # |
| 37 | |
| 38 | # ----- *ADVANCED* CONFIGURATION ----- |
| 39 | # Only change these options if you know what you are doing! |
| 40 | # |
| 41 | # PATH should only include /usr/* if it runs after the mountnfs.sh script. |
| 42 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
| 43 | DESC="bittorrent client" |
| 44 | NAME=transmission-daemon |
| 45 | DAEMON=$(which $NAME) |
| 46 | PIDFILE=/var/run/$NAME.pid |
| 47 | SCRIPTNAME=/etc/init.d/$NAME |
| 48 | |
| 49 | # Exit if the package is not installed |
| 50 | [ -x "$DAEMON" ] || exit 0 |
| 51 | |
| 52 | # Read configuration variable file if it is present |
| 53 | [ -r /etc/default/$NAME ] && . /etc/default/$NAME |
| 54 | |
| 55 | # Load the VERBOSE setting and other rcS variables |
| 56 | [ -f /etc/default/rcS ] && . /etc/default/rcS |
| 57 | |
| 58 | # Define LSB log_* functions. |
| 59 | # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. |
| 60 | . /lib/lsb/init-functions |
| 61 | |
| 62 | # |
| 63 | # Function that starts the daemon/service |
| 64 | # |
| 65 | |
| 66 | do_start() |
| 67 | { |
| 68 | # Export the configuration/web directory, if set |
| 69 | if [ "$TRANSMISSION_HOME" ]; then |
| 70 | export TRANSMISSION_HOME |
| 71 | fi |
| 72 | if [ "$TRANSMISSION_WEB_HOME" ]; then |
| 73 | export TRANSMISSION_WEB_HOME |
| 74 | fi |
| 75 | |
| 76 | # Return |
| 77 | # 0 if daemon has been started |
| 78 | # 1 if daemon was already running |
| 79 | # 2 if daemon could not be started |
| 80 | start-stop-daemon --chuid $USERNAME --start --pidfile $PIDFILE --exec $DAEMON \ |
| 81 | --test -- $TRANSMISSION_ARGS > /dev/null \ |
| 82 | || return 1 |
| 83 | start-stop-daemon --chuid $USERNAME --start --pidfile $PIDFILE --exec $DAEMON |
| 84 | || return 2 |
| 85 | } |
| 86 | |
| 87 | # |
| 88 | # Function that stops the daemon/service |
| 89 | # |
| 90 | do_stop() |
| 91 | { |
| 92 | # Return |
| 93 | # 0 if daemon has been stopped |
| 94 | # 1 if daemon was already stopped |
| 95 | # 2 if daemon could not be stopped |
| 96 | # other if a failure occurred |
| 97 | start-stop-daemon --stop --quiet --retry=TERM/10/KILL/5 --pidfile $PIDFILE --name $NAME |
| 98 | RETVAL="$?" |
| 99 | [ "$RETVAL" = 2 ] && return 2 |
| 100 | |
| 101 | # Wait for children to finish too if this is a daemon that forks |
| 102 | # and if the daemon is only ever run from this initscript. |
| 103 | # If the above conditions are not satisfied then add some other code |
| 104 | # that waits for the process to drop all resources that could be |
| 105 | # needed by services started subsequently. A last resort is to |
| 106 | # sleep for some time. |
| 107 | |
| 108 | start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON |
| 109 | [ "$?" = 2 ] && return 2 |
| 110 | |
| 111 | # Many daemons don't delete their pidfiles when they exit. |
| 112 | rm -f $PIDFILE |
| 113 | |
| 114 | return "$RETVAL" |
| 115 | |
| 116 | } |
| 117 | |
| 118 | # |
| 119 | # Function that sends a SIGHUP to the daemon/service |
| 120 | # |
| 121 | do_reload() { |
| 122 | # |
| 123 | # If the daemon can reload its configuration without |
| 124 | # restarting (for example, when it is sent a SIGHUP), |
| 125 | # then implement that here. |
| 126 | # |
| 127 | start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME |
| 128 | return 0 |
| 129 | } |
| 130 | |
| 131 | case "$1" in |
| 132 | start) |
| 133 | log_daemon_msg "Starting $DESC" "$NAME" |
| 134 | do_start |
| 135 | case "$?" in |
| 136 | 0|1) log_end_msg 0 ;; |
| 137 | 2) log_end_msg 1 ;; |
| 138 | esac |
| 139 | ;; |
| 140 | stop) |
| 141 | log_daemon_msg "Stopping $DESC" "$NAME" |
| 142 | do_stop |
| 143 | case "$?" in |
| 144 | 0|1) log_end_msg 0 ;; |
| 145 | 2) log_end_msg 1 ;; |
| 146 | esac |
| 147 | ;; |
| 148 | restart|force-reload) |
| 149 | # |
| 150 | # If the "reload" option is implemented then remove the |
| 151 | # 'force-reload' alias |
| 152 | # |
| 153 | log_daemon_msg "Restarting $DESC" "$NAME" |
| 154 | do_stop |
| 155 | case "$?" in |
| 156 | 0|1) |
| 157 | do_start |
| 158 | case "$?" in |
| 159 | 0) log_end_msg 0 ;; |
| 160 | 1) log_end_msg 1 ;; # Old process is still running |
| 161 | *) log_end_msg 1 ;; # Failed to start |
| 162 | esac |
| 163 | ;; |
| 164 | *) |
| 165 | # Failed to stop |
| 166 | log_end_msg 1 |
| 167 | ;; |
| 168 | esac |
| 169 | ;; |
| 170 | *) |
| 171 | echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 |
| 172 | exit 3 |
| 173 | ;; |
| 174 | esac |
| 175 | |
| 176 | : |
| 177 | }}} |