Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ### BEGIN INIT INFO
- # Provides: rtorrent
- # Required-Start: $remote_fs $syslog
- # Required-Stop: $remote_fs $syslog
- # Should-Start: $portmap
- # Should-Stop: $portmap
- # X-Start-Before: nis
- # X-Stop-After: nis
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # X-Interactive: false
- # Short-Description: Initscript for rtorrent
- # Description: This file should be used to start|stop rtorrent
- ### END INIT INFO
- ##Start Configuration##
- PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
- DESC="rtorrent"
- NAME="rtorrent"
- DAEMON=$NAME
- SCRIPTNAME=$0
- # file to log to (makes for easier debugging if something goes wrong)
- logfile="/var/log/rtorrentInit.log"
- # Users list
- users="alex liza"
- ###END CONFIGURATION###
- 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 rtorrent binary in PATH $PATH" | tee -a "$logfile" >&2
- exit 3
- fi
- if ! [ -r "${config}" ] ; then
- echo "cannot find readable config ${config}. check that it is there and permissions are appropriate" | tee -a "$logfile" >&2
- exit 3
- fi
- session=`getsession "$config"`
- if ! [ -d "${session}" ] ; then
- echo "cannot find readable session directory ${session} from config ${config}. check permissions" | tee -a "$logfile" >&2
- exit 3
- fi
- }
- d_start() {
- for user in $users; do
- # the full path to the filename where you store your rtorrent configuration
- config="`su -c 'echo $HOME' $user`/.rtorrent.rc"
- # set of options to run with
- options=""
- # default directory for screen, needs to be an absolute path
- base="`su -c 'echo $HOME' $user`"
- # name of screen session
- srnname="rtorrent"
- echo "Start for $user; Config='$config'; Base='$base'" | tee -a "$logfile"
- checkcnfg
- [ -d "${base}" ] && cd "${base}"
- stty stop undef && stty start undef
- su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "screen -dm -S ${srnname} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
- # this works for the screen command, but starting rtorrent below adopts screen session gid
- # even if it is not the screen session we started (e.g. running under an undesirable gid
- #su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "sg \"$group\" -c \"screen -fn -dm -S ${srnname} 2>&1 1>/dev/null\"" ${user} | tee -a "$logfile" >&2
- su -c "screen -S "${srnname}" -X screen rtorrent ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
- done
- }
- d_stop() {
- for user in $users; do
- # the full path to the filename where you store your rtorrent configuration
- config="`su -c 'echo $HOME' $user`/.rtorrent.rc"
- # set of options to run with
- options=""
- # default directory for screen, needs to be an absolute path
- base="`su -c 'echo $HOME' $user`"
- # name of screen session
- srnname="rtorrent"
- checkcnfg
- session=`getsession "$config"`
- if ! [ -s ${session}/rtorrent.lock ] ; then
- return
- fi
- pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
- if ps -A | grep -sq ${pid}.*rtorrent ; then # make sure the pid doesn't belong to another process
- kill -s INT ${pid}
- fi
- done
- }
- getsession() {
- session=`cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" `
- echo $session
- }
- case "$1" in
- start)
- echo -n "Starting $DESC: $NAME"
- d_start
- echo "."
- ;;
- stop)
- echo -n "Stopping $DESC: $NAME"
- d_stop
- echo "."
- ;;
- restart|force-reload)
- echo -n "Restarting $DESC: $NAME"
- d_stop
- sleep 1
- d_start
- echo "."
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement