-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtorrent
105 lines (93 loc) · 3.07 KB
/
rtorrent
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
#!/bin/sh
### BEGIN INIT INFO
# Provides: rtorrentd
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop/Restart rTorrent as a service.
### END INIT INFO
### Variables to help to color the output messages
#-----------------------------------------------------------------------------------------
txtred=$(tput setaf 1) # Red
txtgrn=$(tput setaf 2) # Green
txtylw=$(tput setaf 3) # Yellow
txtrst=$(tput sgr0) # Text reset to bash default
### Service Configuration
#-----------------------------------------------------------------------------------------
# rTorrent Configurations
svcUser="root" # User running the instance of rtorrent
svcBin=`which rtorrent` # rtorrent binary location
svcConf=`su $svcUser -c "echo $HOME"`/.rtorrent.rc # configuration for rtorrent
svcDtachFile=`su $svcUser -c "echo $HOME"`/rtorrent.dtach # dtatch session file
### Functions
#-----------------------------------------------------------------------------------------
chkcfg(){
command -v $svcBin >/dev/null 2>&1 || { echo >&2 "[$txtylw Warn $txtrst] I require$txtylw rtorrent$txtrst but it's not installed or was not found. Aborting."; exit 1; }
command -v dtach >/dev/null 2>&1 || { echo >&2 "[$txtylw Warn $txtrst] I require$txtylw dtach$txtrst but it's not installed or was not found. Aborting."; exit 1; }
if [ ! -r $svcConf ]; then
echo "[$txtred Error $txtrst] Configuration file $txtylw$svcConf$txtrst is missing or it's unreadable by the service user $txtylw$svcUser$txtrst. Aborting."
exit 1
fi
}
chksvc(){
rtPID=$(pidof -s $svcBin)
}
start(){
chksvc
if [ $rtPID ]; then
echo "[$txtylw Warn $txtrst] rtorrent already running: PID $rtPID"
exit 1
else
if [ -S $svcDtachFile ]; then
echo "[$txtylw Warn $txtrst] Unclean daemon shutdown was detected, cleaning previous session rtorrent dtach file ..."
unlink $svcDtachFile
fi
su -l $svcUser -c "dtach -n $svcDtachFile $svcBin -n -o import=$svcConf"
chksvc
if [ $rtPID ]; then
echo "[$txtgrn Ok $txtrst] rtorrent running: PID $rtPID"
fi
fi
}
stop(){
chksvc
if [ ! $rtPID ]; then
echo "[$txtylw Warn $txtrst] Unable to find any running rtorrent instance"
exit 1
else
echo "[$txtgrn OK $txtrst] rtorrent Daemon stopped"
kill -9 $rtPID
fi
}
status(){
chksvc
if [ $rtPID ]; then
echo "[$txtgrn Ok $txtrst] rtorrent is up and running: PID $rtPID"
else
echo "[$txtylw WARN $txtrst] Could not find a running rtorrent daemon"
fi
}
### Code execution
#---------------------------------------------------------------------------------------
rtPID=0
chkcfg
case $1 in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'status')
status
;;
*)
echo "Daemon usage: $0 {start|stop|restart|status}"
;;
esac
exit 0