| 1 | #!/bin/bash |
|---|
| 2 | ############# |
|---|
| 3 | ###<Notes>### |
|---|
| 4 | ############# |
|---|
| 5 | # This script depends on screen. |
|---|
| 6 | # For the stop function to work, you must set an |
|---|
| 7 | # explicit session directory using absolute paths (no, ~ is not absolute) in your rtorrent.rc. |
|---|
| 8 | # If you typically just start rtorrent with just "rtorrent" on the |
|---|
| 9 | # command line, all you need to change is the "user" option. |
|---|
| 10 | # Attach to the screen session as your user with |
|---|
| 11 | # "screen -dr rtorrent". Change "rtorrent" with srnname option. |
|---|
| 12 | # Licensed under the GPLv2 by lostnihilist: lostnihilist _at_ gmail _dot_ com |
|---|
| 13 | ############## |
|---|
| 14 | ###</Notes>### |
|---|
| 15 | ############## |
|---|
| 16 | |
|---|
| 17 | ####################### |
|---|
| 18 | ##Start Configuration## |
|---|
| 19 | ####################### |
|---|
| 20 | # You can specify your configuration in a different file |
|---|
| 21 | # (so that it is saved with upgrades, saved in your home directory, |
|---|
| 22 | # or whatever reason you want to) |
|---|
| 23 | # by commenting out/deleting the configuration lines and placing them |
|---|
| 24 | # in a text file (say /home/user/.rtorrent.init.conf) exactly as you would |
|---|
| 25 | # have written them here (you can leave the comments if you desire |
|---|
| 26 | # and then uncommenting the following line correcting the path/filename |
|---|
| 27 | # for the one you used. note the space after the ".". |
|---|
| 28 | # . /etc/rtorrent.init.conf |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | #Do not put a space on either side of the equal signs e.g. |
|---|
| 32 | # user = user |
|---|
| 33 | # will not work |
|---|
| 34 | # system user to run as (can only use one) |
|---|
| 35 | user="user" |
|---|
| 36 | |
|---|
| 37 | # system user to run as # not implemented, see d_start for beginning implementation |
|---|
| 38 | # group=$(id -ng "$user") |
|---|
| 39 | |
|---|
| 40 | # the full path to the filename where you store your rtorrent configuration |
|---|
| 41 | # must keep parentheses around the entire statement, quotations around each config file |
|---|
| 42 | config=("$(su -c 'echo $HOME' $user)/.rtorrent.rc") |
|---|
| 43 | # Examples: |
|---|
| 44 | # config=("/home/user/.rtorrent.rc") |
|---|
| 45 | # config=("/home/user/.rtorrent.rc" "/mnt/some/drive/.rtorrent2.rc") |
|---|
| 46 | # config=("/home/user/.rtorrent.rc" |
|---|
| 47 | # "/mnt/some/drive/.rtorrent2.rc" |
|---|
| 48 | # "/mnt/another/drive/.rtorrent3.rc") |
|---|
| 49 | |
|---|
| 50 | # set of options to run with each instance, separated by a new line |
|---|
| 51 | # must keep parentheses around the entire statement |
|---|
| 52 | #if no special options, specify with: "" |
|---|
| 53 | options=("") |
|---|
| 54 | # Examples: |
|---|
| 55 | # starts one instance, sourcing both .rtorrent.rc and .rtorrent2.rc |
|---|
| 56 | # options=("-o import=~/.rtorrent2.rc") |
|---|
| 57 | # starts two instances, ignoring .rtorrent.rc for both, and using |
|---|
| 58 | # .rtorrent2.rc for the first, and .rtorrent3.rc for the second |
|---|
| 59 | # we do not check for valid options |
|---|
| 60 | # options=("-n -o import=~/.rtorrent2.rc" "-n -o import=~/rtorrent3.rc") |
|---|
| 61 | |
|---|
| 62 | # default directory for screen, needs to be an absolute path |
|---|
| 63 | base=$(su -c 'echo $HOME' $user) |
|---|
| 64 | |
|---|
| 65 | # name of screen session |
|---|
| 66 | srnname="rtorrent" |
|---|
| 67 | |
|---|
| 68 | # file to log to (makes for easier debugging if something goes wrong) |
|---|
| 69 | logfile="/var/log/rtorrentInit.log" |
|---|
| 70 | ####################### |
|---|
| 71 | ###END CONFIGURATION### |
|---|
| 72 | ####################### |
|---|
| 73 | |
|---|
| 74 | PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin |
|---|
| 75 | DESC="rtorrent" |
|---|
| 76 | NAME=rtorrent |
|---|
| 77 | DAEMON=$NAME |
|---|
| 78 | SCRIPTNAME=/etc/init.d/$NAME |
|---|
| 79 | |
|---|
| 80 | checkcnfg() { |
|---|
| 81 | exists=0 |
|---|
| 82 | for i in `echo "$PATH" | tr ':' '\n'` ; do |
|---|
| 83 | if [ -f $i/$NAME ] ; then |
|---|
| 84 | exists=1 |
|---|
| 85 | break |
|---|
| 86 | fi |
|---|
| 87 | done |
|---|
| 88 | if [ $exists -eq 0 ] ; then |
|---|
| 89 | echo "cannot find $NAME binary in PATH: $PATH" | tee -a "$logfile" >&2 |
|---|
| 90 | exit 3 |
|---|
| 91 | fi |
|---|
| 92 | for (( i=0 ; i < ${#config[@]} ; i++ )) ; do |
|---|
| 93 | if ! [ -r "${config[i]}" ] ; then |
|---|
| 94 | echo "cannot find readable config ${config[i]}. check that it is there and permissions are appropriate" | tee -a "$logfile" >&2 |
|---|
| 95 | exit 3 |
|---|
| 96 | fi |
|---|
| 97 | session=$(getsession "${config[i]}") |
|---|
| 98 | if ! [ -d "${session}" ] ; then |
|---|
| 99 | echo "cannot find readable session directory ${session} from config ${config[i]}. check permissions" | tee -a "$logfile" >&2 |
|---|
| 100 | exit 3 |
|---|
| 101 | fi |
|---|
| 102 | done |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | d_start() { |
|---|
| 106 | [ -d "${base}" ] && cd "${base}" |
|---|
| 107 | stty stop undef && stty start undef |
|---|
| 108 | su -c "screen -S "${srnname}" -X screen rtorrent ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2 |
|---|
| 109 | # this works for the screen command, but starting rtorrent below adopts screen session gid |
|---|
| 110 | # even if it is not the screen session we started (e.g. running under an undesirable gid |
|---|
| 111 | #su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "sg \"$group\" -c \"screen -fn -dm -S ${srnname} 2>&1 1>/dev/null\"" ${user} | tee -a "$logfile" >&2 |
|---|
| 112 | for (( i=0 ; i < ${#options[@]} ; i++ )) ; do |
|---|
| 113 | sleep 3 |
|---|
| 114 | su -c "screen -S "${srnname}" -X screen rtorrent ${options[i]} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2 |
|---|
| 115 | done |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | d_stop() { |
|---|
| 119 | for (( i=0 ; i < ${#config[@]} ; i++ )) ; do |
|---|
| 120 | session=$(getsession "${config[i]}") |
|---|
| 121 | if ! [ -s ${session}/rtorrent.lock ] ; then |
|---|
| 122 | return |
|---|
| 123 | fi |
|---|
| 124 | pid=$(cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g") |
|---|
| 125 | # make sure the pid doesn't belong to another process |
|---|
| 126 | if ps -A | grep -sq ${pid}.*rtorrent ; then |
|---|
| 127 | kill -s INT ${pid} |
|---|
| 128 | fi |
|---|
| 129 | done |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | getsession() { |
|---|
| 133 | session=$(cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" ) |
|---|
| 134 | #session=${session/#~/`getent passwd ${user}|cut -d: -f6`} |
|---|
| 135 | echo $session |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | checkcnfg |
|---|
| 139 | |
|---|
| 140 | case "$1" in |
|---|
| 141 | start) |
|---|
| 142 | echo -n "Starting $DESC: $NAME" |
|---|
| 143 | d_start |
|---|
| 144 | echo "." |
|---|
| 145 | ;; |
|---|
| 146 | stop) |
|---|
| 147 | echo -n "Stopping $DESC: $NAME" |
|---|
| 148 | d_stop |
|---|
| 149 | echo "." |
|---|
| 150 | ;; |
|---|
| 151 | restart|force-reload) |
|---|
| 152 | echo -n "Restarting $DESC: $NAME" |
|---|
| 153 | d_stop |
|---|
| 154 | sleep 1 |
|---|
| 155 | d_start |
|---|
| 156 | echo "." |
|---|
| 157 | ;; |
|---|
| 158 | *) |
|---|
| 159 | echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 |
|---|
| 160 | exit 1 |
|---|
| 161 | ;; |
|---|
| 162 | esac |
|---|
| 163 | |
|---|
| 164 | exit 0 |
|---|