Advertisement
Guest User

check-rtorrent

a guest
Oct 19th, 2010
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #!/bin/bash
  2. ####################################################################################
  3. # #
  4. # Simple Bash shell script that will check for rtorrent processes of all users. #
  5. # #
  6. ####################################################################################
  7.  
  8. UHOME=/home
  9. SUBDIR=/rtorrent
  10. PIDFILE=/.session/rtorrent.lock
  11. RINITD=/etc/init.d/rtorrent-
  12. USERS=( $(grep 10[0-9][0-9] /etc/passwd | cut -d: -f1) )
  13. NUM=${#USERS[@]}
  14.  
  15. for (( i=0; i<NUM; i++ ))
  16. do
  17. # Check if username has rtorrent directory
  18. if [ -d ${UHOME}/${USERS[${i}]}${SUBDIR} ]; then
  19. echo Directory ${UHOME}/${USERS[${i}]}${SUBDIR} exists!
  20.  
  21. # Check if username has rtorrent.lock
  22. if [ -e ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE} ]; then
  23. echo File ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE} exists!
  24.  
  25. # Get the PID of user's rtorrent from rtorrent.lock
  26. RPID=`cut -d+ -f2 ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE}`
  27. # echo RPID = $RPID
  28.  
  29. # Check if that PID is running
  30. kill -0 $RPID > /dev/null 2>&1
  31. if [ $? != 0 ]; then
  32. echo ${USERS[${i}]}\'s rtorrent is not running...
  33. echo Attempting to start ${RINITD}${USERS[${i}]}...
  34. echo Deleting ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE} first.
  35. rm -f ${UHOME}/${USERS[${i}]}${SUBDIR}${PIDFILE}
  36. ${RINITD}${USERS[${i}]} start
  37. sleep 2
  38.  
  39. if [ $? != 0 ]; then
  40. echo Failed to start ${USERS[${i}]}\'s rtorrent!
  41. else
  42. echo Successfully started ${USERS[${i}]}\'s rtorrent.
  43. fi
  44.  
  45. else
  46. echo ${USERS[${i}]}\'s rtorrent is running...
  47. fi
  48.  
  49. else
  50. # rtorrent.lock does not exist. process may not be running. reloading...
  51. echo ${USERS[${i}]}\'s rtorrent.lock does not exist!
  52.  
  53. # Make sure no stale rtorrent pid is running
  54. SPID=( $(pgrep rtorrent -u ${USERS[${i}]}) )
  55. SPIDNUM=${#SPID[@]}
  56.  
  57. for (( j=0; j<SPIDNUM; j++ ))
  58. do
  59. if [ ${SPID[${j}]} > 0 ]; then
  60. # echo Killing ${USERS[${i}]}\'s stale processes...
  61. kill -9 ${SPID[${j}]}
  62. fi
  63. done
  64.  
  65. # Starting rtorrent
  66. ${RINITD}${USERS[${i}]} start
  67. sleep 2
  68. if [ $? != 0 ]; then
  69. echo Failed to start ${USERS[${i}]}\'s rtorrent!
  70. else
  71. echo Successfully started ${USERS[${i}]}\'s rtorrent.
  72. # Sleeping for 5 seconds before going to the next user
  73. sleep 5
  74. fi
  75. fi
  76.  
  77. else
  78. echo Directory ${UHOME}/${USERS[${i}]}${SUBDIR} does NOT exist!
  79. echo Not processing user ${USERS[${i}]}...
  80. fi
  81. done
  82.  
  83. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement