Автоматизация загрузки торрентов

  1. #!/bin/sh
  2.  
  3. TORRENTS_DIR="/home/zver/torrents"
  4. RUN_FILE="${TORRENTS_DIR}/running.txt"
  5. LOG_FILE="${TORRENTS_DIR}/ctorrent.log"
  6. DOWNLOADED_DIR="${TORRENTS_DIR}/downloaded"
  7. SAVE_DIR="/home/user"
  8. CTORRENT="/usr/local/bin/ctorrent"
  9.  
  10. if [ ! -x "${CTORRENT}" ]; then
  11.   echo "ctorrent binary not found"
  12.   exit 1
  13. fi
  14.  
  15. if [ ! -d "${DOWNLOADED_DIR}" ]; then
  16.   mkdir -p "$DOWNLOADED_DIR"
  17. fi
  18.  
  19. for trfile in `ls "${TORRENTS_DIR}" | grep -E '\.torrent$'`; do
  20.   if [ -f "${RUN_FILE}" ]; then
  21.     if [ ! -z "`cat "${RUN_FILE}" | grep -F "${trfile}"`" ]; then
  22.       ctorrent_run=`ps -ax | grep ctorrent`
  23.       if [ -z "${ctorrent_run}" ]; then
  24.         rm -f "$RUN_FILE" "$LOG_FILE"
  25.         mv "${TORRENTS_DIR}/${trfile}" "${DOWNLOADED_DIR}"
  26.         continue
  27.       else
  28.         break
  29.       fi
  30.     else
  31.       continue
  32.     fi
  33.   else
  34.     ${CTORRENT} -a -e 0 -C 32 -U 32 "${TORRENTS_DIR}/${trfile}" > ${LOG_FILE} 2>&1 &
  35.     echo "${trfile}" > "${RUN_FILE}"
  36.     break
  37.   fi
  38. done
  39.  
  40. exit 0