#!/bin/bash # # rtorrent Startup script for rTorrent # # chkconfig: - 90 15 # description: rTorrent is the console torrent client # config: /home/rtorrent/.rc. # Source Red Hat function library. . /etc/rc.d/init.d/functions RETVAL=0 PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin NAME=rtorrent USER="rtorrent" CONFIG=("$(su -c 'echo $HOME' $USER)/.rtorrent.rc") LOG_FILE=/var/log/$NAME.log LOCK_FILE=/var/lock/subsys/$NAME checkcnfg() { exists=0 for i in `echo "$PATH" | tr ':' '\n'` ; do if [ -f $i/$NAME ] ; then exists=1 break fi done if [ $exists -eq 0 ] ; then echo "cannot find $NAME binary in PATH: $PATH" | tee -a "$LOG_FILE" >&2 exit 3 fi for (( i=0 ; i < ${#CONFIG[@]} ; i++ )) ; do if ! [ -r "${CONFIG[i]}" ] ; then echo "cannot find readable config ${CONFIG[i]}. check that it is there and permissions are appropriate" | tee -a "$LOG_FILE" >&2 exit 3 fi session=$(getsession "${CONFIG[i]}") if ! [ -d "${session}" ] ; then echo "cannot find readable session directory ${session} from config ${CONFIG[i]}. check permissions" | tee -a "$LOG_FILE" >&2 exit 3 fi done } checkscreen () { if [ ! -f /usr/bin/screen ] ; then echo "\"screen\" program is not installed! Please install it. You can do it if run \"\# yum install screen\"" exit 3 fi } start() { echo -n $"Starting $NAME: " checkcnfg || checkscreen || exit 1 daemon "su $NAME -c 'screen -d -m rtorrent'" RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $LOCK_FILE return $RETVAL } stop() { echo -n $"Stopping $NAME: " killproc rtorrent su $NAME -c 'screen -wipe' >/dev/null 2>&1 RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f $LOCK_FILE } getsession() { session=$(cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" ) #session=${session/#~/`getent passwd ${user}|cut -d: -f6`} echo $session } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; *) echo $"Usage: $NAME {start|stop|restart}" >&2 exit 1 esac exit $RETVAL