Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ####################################################################################
- # #
- # Simple Bash shell script that will check for rtorrent processes of all users. #
- # #
- ####################################################################################
- UHOME=/home
- SUBDIR=/rtorrent
- PIDFILE=/.session/rtorrent.lock
- RINITD=/etc/init.d/rtorrent-
- USERS=( $(grep 10[0-9][0-9] /etc/passwd | cut -d: -f1) )
- NUM=${#USERS[@]}
- for (( i=0; i<NUM; i++ ))
- do
- # Check if username has rtorrent directory
- if [ -d ${UHOME}/${USERS[${i}]}${SUBDIR} ]; then
- echo Directory ${UHOME}/${USERS[${i}]}${SUBDIR} exists!
- # Check if username has rtorrent.lock
- if [ -e ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE} ]; then
- echo File ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE} exists!
- # Get the PID of user's rtorrent from rtorrent.lock
- RPID=`cut -d+ -f2 ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE}`
- # echo RPID = $RPID
- # Check if that PID is running
- kill -0 $RPID > /dev/null 2>&1
- if [ $? != 0 ]; then
- echo ${USERS[${i}]}\'s rtorrent is not running...
- echo Attempting to start ${RINITD}${USERS[${i}]}...
- echo Deleting ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE} first.
- rm -f ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE}
- ${RINITD}${USERS[${i}]} start
- sleep 2
- if [ $? != 0 ]; then
- echo Failed to start ${USERS[${i}]}\'s rtorrent!
- else
- echo Successfully started ${USERS[${i}]}\'s rtorrent.
- fi
- else
- echo ${USERS[${i}]}\'s rtorrent is running...
- fi
- else
- # rtorrent.lock does not exist. process may not be running. reloading...
- echo ${USERS[${i}]}\'s rtorrent.lock does not exist!
- # Make sure no stale rtorrent pid is running
- SPID=( $(pgrep rtorrent -u ${USERS[${i}]}) )
- SPIDNUM=${#SPID[@]}
- for (( j=0; j<SPIDNUM; j++ ))
- do
- if [ ${SPID[${j}]} > 0 ]; then
- # echo Killing ${USERS[${i}]}\'s stale processes...
- kill -9 ${SPID[${j}]}
- fi
- done
- # Starting rtorrent
- ${RINITD}${USERS[${i}]} start
- sleep 2
- if [ $? != 0 ]; then
- echo Failed to start ${USERS[${i}]}\'s rtorrent!
- else
- echo Successfully started ${USERS[${i}]}\'s rtorrent.
- # Sleeping for 5 seconds before going to the next user
- sleep 5
- fi
- fi
- else
- echo Directory ${UHOME}/${USERS[${i}]}${SUBDIR} does NOT exist!
- echo Not processing user ${USERS[${i}]}...
- fi
- done
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement