-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbittorrent
executable file
·230 lines (200 loc) · 5.73 KB
/
bittorrent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
# /etc/init.d/bittorrent
# version 2012-07-17 (YYYY-MM-DD)
### BEGIN INIT INFO
# Provides: bittorrent
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: rTorrent client
# Description: Starts the rTorrent BitTorrent client
### END INIT INFO
# rtorrent-init-script - An initscript to start rTorrent on CentOS
# Copyright (C) 2011 - Super Jamie <jamie@superjamie.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Source function library
. /etc/rc.d/init.d/functions
## Settings
# Username of non-root user who will run the server
USERNAME="username"
# Nice looking name of service for script to report back to users
SERVERNAME="rTorrent"
# Filename of server binary
SERVICE="rtorrent"
# Path where config file and session directory can be found
BASEPATH="/home/${USERNAME}"
# Name of configuration file
CONFIGFILE=".rtorrent.rc"
# Sets location of config file
CONFIGPATH="${BASEPATH}/${CONFIGFILE}"
# Name of screen session
SCRNAME="rtorrent"
# Set of options to run rtorrent with
OPTIONS=""
## End Settings
## Functions
# Run all commands as the non-root user
as_user() {
ME=`whoami`
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
# Check if rTorrent is running. If not, say so and exit the script.
check_running() {
get_session;
if ! [ -s ${SESSIONPATH}/rtorrent.lock ];
then
echo "* ${SERVERNAME} is not running."
exit 1;
fi
}
# Get the Process ID of rTorrent from the lockfile
get_pid() {
PID=`cat ${SESSIONPATH}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
}
# Get the session path from the configfile. REQUIRES ABSOLUTE PATH! eg: session = /home/username/.session
get_session() {
SESSIONPATH=`cat ${CONFIGPATH} | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//"`
}
# Check the rTorrent binary is in the path, check the config file is readable, check the session dir is readable
check_files() {
RTORRENTEXISTS=0
for i in `echo "$PATH" | tr ':' '\n'`;
do
if [ -f ${i}/${SERVICE} ];
then
RTORRENTEXISTS=1
break;
fi
done
if [ ${RTORRENTEXISTS} -eq 0 ];
then
echo "* Cannot find ${SERVERNAME} binary in PATH ${PATH}"
exit 1;
fi
if ! [ -r "${CONFIGPATH}" ];
then
echo "* Cannot find readable config file ${CONFIGPATH}"
exit 1;
fi
get_session;
if ! [ -d "${SESSIONPATH}" ];
then
echo "* Cannot find readable session directory ${SESSIONPATH} from config file ${CONFIGFILE}"
exit 1;
fi
}
# Start rTorrent as a service within a screen session
do_start() {
check_files;
get_session;
if ! [ -s ${SESSIONPATH}/rtorrent.lock ];
then
echo "* ${SERVERNAME} was not already running. Starting..."
# -c /dev/null : run screen with no config file, the user could already have one which breaks things
# -fn : turn flow-control off so ctrl+q works to quit
# -dmS : start a detatched screen called $SCRNAME with $SERVICE $OPTIONS inside it
as_user "cd ${BASEPATH} && screen -c /dev/null -fn -dmS ${SCRNAME} ${SERVICE} ${OPTIONS} 2>&1 1>/dev/null"
echo "* Checking ${SERVERNAME} is running..."
sleep 2;
do_status;
else
get_pid;
echo "* ${SERVERNAME} was already running! (pid ${PID})"
exit 1;
fi
}
do_stop() {
check_running;
echo "* Attempting to stop ${SERVERNAME}..."
get_pid;
# make sure the pid doesn't belong to another process
if ps -A | grep -sq ${PID}.*${SERVICE};
then
kill -s INT ${PID}
else
echo "* Failed to stop ${SERVERNAME}. Process ${PID} belongs to something else. Manual intervention required."
exit 1;
fi
# when rTorrent gets SIGINT it waits 5 seconds to send totals to trackers before quitting, so we sleep 5 here
sleep 5;
get_session;
if ! [ -s ${SESSIONPATH}/rtorrent.lock ];
then
echo "* ${SERVERNAME} has been stopped."
else
get_pid;
echo "* ${SERVERNAME} is still running! (pid ${PID})"
fi
}
# Check if process is running and return PID
do_status() {
check_running;
get_pid;
echo "* ${SERVERNAME} is running. (pid ${PID})"
}
# Give a bit more information than status
do_info() {
check_running;
get_pid;
RSS="`ps -p ${PID} --format rss | tail -n 1 | awk '{print $1}'`"
echo "- Base Path : ${BASEPATH}"
echo "- Config File : ${CONFIGPATH}"
echo "- Screen Session Name : ${SCRNAME}"
echo "- rTorrent Session Path : ${SESSIONPATH}"
echo "- Process ID : "${PID}""
echo "- Memory Usage : `expr ${RSS} / 1024` Mb (${RSS} kb)"
echo "- Active Connections : "
netstat --ip -anp | grep -E "Proto|${SERVICE}"
}
# Connect to the active Screen session. Disconnect with Ctrl+a then d
do_connect() {
check_running;
as_user "screen -dr ${SCRNAME}"
}
## These are the parameters passed to the script
case "$1" in
'start')
do_start;
;;
'stop')
do_stop;
;;
'restart')
do_stop;
sleep 1
do_start;
;;
'status')
do_status;
;;
'info')
do_info
;;
'connect')
do_connect;
;;
*)
echo "* Usage: bittorrent {start|stop|restart|status|info|connect}"
exit 1;
;;
esac
exit 0;