rtorrent as a Service

lisas4567

Member
May 25, 2018
773
0
16
Probably there are tons of scripts like mine but i figured out to share my version of rtorrent Daemon, this been said in order to learn more about bash scripting any critics or constructive feedback is always welcomed. PS: This was made and tested in Debian environment.

Code:
#!/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="rtorrent" # User running the instance of rtorrent
svcBin=/usr/local/bin/rtorrent # rtorrent binary location
svcConf=/home/$svcUser/.rtorrent.rc # configuration for rtorrent
svcDtachFile=/home/$svcUser/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