备份自https://blog.wangyan.org/bt-shell-transmission-clean/
一、删除顺序
- 首先逐个删除已经下载完的任务,一旦满足条件则停止删除
- 接着逐个删除已经暂停的任务,一旦满足条件则停止删除
- 最后按顺序删除正在下载的任务(相当于删除全部任务),直到满足条件。
- 如果还是未能满足条件,则动用”rm -rf”强制删除下载目录内的所有文件。
二、使用方法
2.1、创建脚本并测试
1 2 3 4 |
mkdir ~/trans/ && cd ~/trans/ 新建脚本:nano trans_cleanup.sh 运行权限:chmod +x ./trans_cleanup.sh ./trans_cleanup.sh #先运行一次查看是否正常工作 |
trans_cleanup.sh脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#! /bin/bash #==================================================================== # trans_cleanup.sh # # Copyright (c) 2011, WangYan <webmaster@wangyan.org> # All rights reserved. # Distributed under the GNU General Public License, version 3.0. # # Monitor disk space, If the Over, delete some files. # # See: http://wangyan.org/blog/trans_cleanup.html # # V0.2, since 2012-10-29 #==================================================================== # The transmission remote login username USERNAME="demo" # The transmission remote login password PASSWORD="demo" # The transmission download dir DLDIR="/" # The maximum allowed disk (%) DISK_USED_MAX="90" # Enable auto shutdown support (Disable=0, Enable=1) ENABLE_AUTO_SHUTDOWN="0" # Log path settings LOG_PATH="/var/log/trans_cleanup.log" # Date time format setting DATA_TIME=$(date +"%y-%m-%d %H:%M:%S") #==================================================================== dist_check() { DISK_USED=`df -h / | grep -v Use | awk '{print $5}' | cut -d '%' -f 1` DISK_OVER=`awk 'BEGIN{print('$DISK_USED'>'$DISK_USED_MAX')}'` } dist_check if [ "$DISK_OVER" = "1" ];then for ii in `transmission-remote --auth $USERNAME:$PASSWORD -l | grep Stopped | awk '{print $1}' | grep -v ID | sed 's:*::'` do [ "$ii" -gt "0" ] && echo -n "$DATA_TIME [Stopped] " >> $LOG_PATH transmission-remote --auth $USERNAME:$PASSWORD -t $ii --remove-and-delete >> $LOG_PATH 2>&1 [ "$ii" -gt "0" ] && sleep 10 && dist_check [ "$DISK_OVER" = "0" ] && break done fi if [ "$DISK_OVER" = "1" ];then for iiii in `transmission-remote --auth $USERNAME:$PASSWORD -l | grep Idle | awk '{print $1}' | grep -v ID | sed 's:*::'` do [ "$iiii" -gt "0" ] && echo -n "$DATA_TIME [Idle] " >> $LOG_PATH transmission-remote --auth $USERNAME:$PASSWORD -t $iiii --remove-and-delete >> $LOG_PATH 2>&1 [ "$iiii" -gt "0" ] && sleep 10 && dist_check [ "$DISK_OVER" = "0" ] && break done fi if [ "$DISK_OVER" = "1" ];then for i in `transmission-remote --auth $USERNAME:$PASSWORD -l | grep 100% | grep Done | awk '{print $1}' | grep -v ID | sed 's:*::'` do [ "$i" -gt "0" ] && echo -n "$DATA_TIME [Done] " >> $LOG_PATH transmission-remote --auth $USERNAME:$PASSWORD -t $i --remove-and-delete >> $LOG_PATH 2>&1 [ "$i" -gt "0" ] && sleep 10 && dist_check [ "$DISK_OVER" = "0" ] && break done fi if [ "$DISK_OVER" = "1" ];then for iii in `transmission-remote --auth $USERNAME:$PASSWORD -l | grep -v Sum | awk '{print $1}' | grep -v ID | sed 's:*::'` do [ "$iii" -gt "0" ] && echo -n "$DATA_TIME [Up or Down] " >> $LOG_PATH transmission-remote --auth $USERNAME:$PASSWORD -t $iii --remove-and-delete >> $LOG_PATH 2>&1 [ "$iii" -gt "0" ] && sleep 10 && dist_check [ "$DISK_OVER" = "0" ] && break done fi if [ "$DISK_OVER" = "1" ];then rm -rf $DLDIR/* fi if [ "$ENABLE_AUTO_SHUTDOWN" = "1" ];then SHUTDOWN=1 for STATUS in `transmission-remote --auth $USERNAME:$PASSWORD -l | awk '{print $9}'` do if [[ "$STATUS" = "Up" || "$STATUS" = "Uploading" ]];then SHUTDOWN=0 fi done TASK_TOTAL=`transmission-remote --auth $USERNAME:$PASSWORD -l | grep -Ev '(ID|Sum)' | wc -l` if [ "$TASK_TOTAL" -gt "0" ] && [ "$SHUTDOWN" -eq "1" ];then echo -n "$DATA_TIME " >> $LOG_PATH shutdown now >> $LOG_PATH 2>&1 fi fi |
2.2、设置cron计划任务
自动检查频率可随意设置,例如每5分钟检查一次
1 2 |
crontab -e */5 * * * * /bin/bash /root/trans/trans_cleanup.sh |
三、配置选项
用编辑器打开trans_cleanup.sh文件,自定义以下选项
1)登录用户名,默认是demo
1 |
USERNAME="demo" |
2) 登录密码,默认是demo
1 |
PASSWORD="domo" |
3) transmission下载目录,默认在/root/Downloads
1 |
DLDIR=/root/Downloads |
4) 磁盘最大使用阀值,默认95%
1 |
DISK_USED_MAX="90" |
5) 日志存放路径,默认在/var/log/trans_cleanup.log
1 |
LOG_PATH="/var/log/trans_cleanup.log" |
6) 日志日期格式,一般不需要改。
1 |
DATA_TIME=$(date +"%y-%m-%d %H:%M:%S") |