forked from arakasi72/rtinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rt
243 lines (214 loc) · 5.41 KB
/
rt
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
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/bin/bash
######################################################################
#
# Copyright (c) 2015 arakasi72 (https://github.com/arakasi72)
#
# --> Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
######################################################################
# rTorrent Stop/Start/Restart
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
FILE="$HOME/rtorrent/.session/rtorrent.lock"
SERVICE='rtorrent'
SERVICEB='irssi'
RTPID=$(pgrep -fx -u $LOGNAME $SERVICE)
# Sets default status can be askkill, autokill, or donotkill
KILLSTATUS='askkill'
KILLSIGNAL='-2'
RTLOG="/dev/null"
loglines=500
useerr=0
LOGFLAG=0
helpflag=1
# write log file
log_header() {
RTLOG="$HOME/rt.log"
echo >> $RTLOG
date >> $RTLOG
echo "$0 $@" >> $RTLOG
if [ $(cat "$RTLOG" | wc -l) -gt $loglines ]; then
tail -$loglines "$RTLOG" > $HOME/rt.temp
cat $HOME/rt.temp > $RTLOG
rm -f $HOME/rt.temp
fi
}
# function to check if service is running
service_running(){
pgrep -fx -u $LOGNAME $SERVICE > /dev/null
}
# function to ask user for y/n response
ask_user(){
if [ -t 1 ]; then
while true
do
read -p "Unable to close $SERVICE, close using kill -9? " answer
case $answer in [Yy]* ) return 0 ;;
[Nn]* ) return 1 ;;
* ) echo "Enter y or n";;
esac
done
else
echo "Running in background -a auto response is y" >> $RTLOG
return 0
fi
}
# function that starts service
start_service(){
local seconds=0
if [ $SERVICE = "rtorrent" ]; then
if [ -a $FILE ]; then
echo "removing $FILE" | tee -a "$RTLOG"
rm -f $FILE
else
echo "No session lock file" | tee -a "$RTLOG"
fi
fi
echo -n "Starting $SERVICE"
screen -d -m -S $SERVICE $SERVICE
while ! (service_running)
do
seconds=$(( $seconds + 1 ))
echo -n "."
sleep 1
if [ $seconds = 10 ]; then
echo
return 1
fi
done
echo
return 0
}
# function that stops service
stop_service(){
local seconds=0
echo -n "$SERVICE shutting down using kill $1"
kill $1 $RTPID
while ( service_running )
do
seconds=$(( $seconds + 1 ))
echo -n " ."
sleep 1
if [ $seconds = 20 ]; then
echo
return 1
fi
done
echo
return 0
}
# reports any usage error
usage_error() {
echo "usage: [-h] [-a|-k|-d] [-l] [start | stop | restart]" | tee -a "$RTLOG"
if [ $1 = 1 ]; then
echo "only one of the options -a -k -d allowed" | tee -a "$RTLOG"
elif [ $1 = 2 ]; then
echo "incorrect option used" | tee -a "$RTLOG"
elif [ $1 = 3 ]; then
echo "incorrect argument used" | tee -a "$RTLOG"
elif [ $1 = 4 ]; then
echo "too many arguments, only 1 argument allowed" | tee -a "$RTLOG"
fi
exit 1
}
# prints usage help to screen
usage_help() {
echo "usage: [-h] [-i] [-a|-k|-d] [-l] [start | stop | restart]"
echo "options:"
echo " -h usage help display"
echo " -i sets service to $SERVICEB"
echo " -l send results to log file (~/rt.log)"
echo " -a if unable to close $SERVICE ask user whether to use kill -9"
echo " -k if unable to close $SERVICE use kill -9"
echo " -d if unable to close $SERVICE do not use kill -9"
echo "arguments:"
echo " start starts $SERVICE"
echo " stop stops $SERVICE"
echo " restart first stops and then starts $SERVICE"
exit 0
}
#exit if run by root
if [ "$LOGNAME" = "root" ]; then
echo "Cannot be run as root, or with sudo"
exit
fi
# Check for options
while getopts ":hlikad" optname
do
case $optname in
"k" )[[ -n $OPTFLAG ]] && useerr=1 || KILLSTATUS='autokill' && OPTFLAG=1 ;;
"a" )[[ -n $OPTFLAG ]] && useerr=1 || KILLSTATUS='askkill' && OPTFLAG=1 ;;
"d" )[[ -n $OPTFLAG ]] && useerr=1 || KILLSTATUS='donotkill' && OPTFLAG=1 ;;
"i" ) SERVICE=$SERVICEB && RTPID=$(pgrep -fx -u $LOGNAME $SERVICE) ;;
"l" ) LOGFLAG=1 ;;
"h" ) helpflag=0 ;;
* ) useerr=2 ;;
esac
done
if [ $helpflag = 0 ]; then
usage_help
fi
if [ $LOGFLAG = 1 ]; then
log_header "$@"
fi
if [ $useerr -gt 0 ]; then
usage_error $useerr
fi
shift $(( $OPTIND - 1 ))
# Check correct arguments used
if [ $# = 1 ]; then
if ! [[ $1 = "stop" || $1 = "start" || $1 = "restart" ]]; then
useerr=3
usage_error $useerr
fi
fi
# Check if there is more than 1 argument
if [ $# -gt 1 ]; then
useerr=4
usage_error $useerr
fi
# Check if service is running
if [ $# = 0 ]; then
if ( service_running ); then
echo "$SERVICE is running"
else
echo "$SERVICE is NOT running"
fi
fi
# Stop Service
if [[ $1 = "stop" || $1 = "restart" ]]; then
if ! ( service_running ); then
echo "$SERVICE was not running"
else
if ! ( stop_service $KILLSIGNAL ); then
case $KILLSTATUS in
"autokill" )
KILLSIGNAL='-9'
stop_service $KILLSIGNAL ;;
"askkill" )
if ( ask_user ); then
KILLSIGNAL='-9'
stop_service $KILLSIGNAL
fi ;;
esac
fi
if ( service_running ); then
echo "WARNING unable to close $SERVICE using $KILLSIGNAL" | tee -a "$RTLOG"
exit 1
else
echo "$SERVICE has been closed using kill $KILLSIGNAL" | tee -a "$RTLOG"
fi
fi
fi
# Start Service
if [[ $1 = "start" || $1 = "restart" ]]; then
if ( service_running ); then
echo "$SERVICE was already running"
else
if ( start_service ); then
echo "$SERVICE has been started" | tee -a "$RTLOG"
else
echo "WARNING: Unable to start $SERVICE" | tee -a "$RTLOG"
fi
fi
fi
echo "There were no problems encountered" >> "$RTLOG"