多个种子的传输关闭脚本?

我写了一个关闭脚本进行传输。 在Torrent下载完成后,传输会调用脚本。 该脚本在我的机器上运行完美(Ubuntu 11.04和12.04)。

#!/bin/bash sleep 300s # default display on current host DISPLAY=:0.0 # find out if monitor is on. Default timeout can be configured from screensaver/Power configuration. STATUS=`xset -display $DISPLAY -q | grep 'Monitor'` echo $STATUS if [ "$STATUS" == " Monitor is On" ] ### Then check if its still downloading a torrent. Couldn't figure out how.(May be) by monitoring network downstream activity? then notify-send "Downloads Complete" "Exiting transmisssion now" pkill transmission else notify-send "Downloads Complete" "Shutting Down Computer" dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.RequestShutdown fi exit 0 

问题是当我下载多个文件时,当第一个文件完成时,传输执行脚本。 我想这样做,但完成所有下载后。

如果它还在下载另一个torrent,我想进行第二次检查(在监视器检查后)。

有没有办法做到这一点?

此信息不会通过环境变量传递给脚本,因此您需要查询Transmission的RPC接口。 这有时由客户端库完成 – 例如,Python脚本可以使用python transmissionrpc 。 http://www.transmissionbt.com/resources/上列出了其他类似的接口。

这是一种快速方法,它将使用transmission-remote来计算非空闲下载的数量:

 transmission-remote yourhost:yourport -tall --info | grep "^ State:" | grep "Down" | wc --lines 

如果你想包括空闲下载,你可以试试这个:

 transmission-remote yourhost:yourport -l | grep -v -e " 100% " -e "^Sum" -e "^ID" -e " Stopped " | wc --lines 

其中“^ ID”和“^ Sum”剥离页眉和页脚; “100%”剥离完成的种子; 并且“停止”消除了不完整但暂停的种子。 这种方法并非万无一失 – 例如,名为“100%已停止”的torrent会破坏它。

我创建了一个更好的脚本(来自user98677建议),它利用了传输的RPC接口。

码:

Github Gist: https : //gist.github.com/khurshid-alam/6474227

它能做什么?

  1. 完成后暂停或移除已完成的种子。

  2. 发送推覆通知(带curl)[可选]

  3. 发送推特通知(需要twidge)[可选]

  4. 暂停/关闭计算机或保持原样。

来自传输的Pushover通知

截图


建立

在Ubuntu上

 sudo apt-get install libnotify-bin sudo apt-get install transmission-cli 

在Ubuntu> = 13.04(对于Twitter通知):

 sudo add-apt-repository ppa:moorhen-core/moorhen-apps sudo apt-get install twidge 

对于非Ubuntu发行版的suspend操作(Ubuntu使用Upower)安装powermanagement-interface包

 sudo apt-get install powermanagement-interface 

安装后:

  1. 从github-gist获取代码并将文件保存为硬盘驱动器上的trsm 。 使文件可执行文件chmod a+x trsm

  2. 登录以切换并复制您的用户密钥 。 将其粘贴到脚本中的user-key下。

  3. 如果你想要通过漂亮的应用程序(传输)图标发送通知,只需在带有传输图标的pushover中创建一个假应用程序,然后复制应用程序密钥(API /令牌密钥)并将其粘贴到脚本中的app-key下。

  4. 对于twitter设置,请参阅twidge文档。

  5. 打开传输。 转到首选项 – >网站。 启用Web客户端(默认端口9091 )并启用用户身份validation。 选择用户名和密码。 将该用户usernamepassword分别作为username名和密码放入脚本中。

  6. 单击打开Web客户端以检查它是否正常工作。

  7. 最后保存脚本并转到下载选项卡(在传输首选项中)并call script when torrent is complete单击call script when torrent is complete 。 选择相应的脚本。


脚本

 #!/bin/bash user-key=" " #put your pushover user-key app-key=" " #put your pushover application-key device=" " #Your device name in pushover username=" " # Transmission remote username password=" " # Transmission remote password sleep 100s # default display on current host DISPLAY=:0.0 # authorize transmission trsm="transmission-remote --auth $username:$password" # find out number of torrent TORRENTLIST=`$trsm --list | sed -e '1d;$d;s/^ *//' | cut --only-delimited --delimiter=' ' --fields=1` for TORRENTID in $TORRENTLIST do echo "* * * * * Operations on torrent ID $TORRENTID starting. * * * * *" #echo $TORRENTID DL_COMPLETED=`$trsm --torrent $TORRENTID --info | grep "Percent Done: 100%"` #echo $DL_COMPLETED # pause completed torrents & get those torrent names. if [ "$DL_COMPLETED" != "" ]; then $trsm --torrent $TORRENTID --stop trname=`$trsm --torrent $TORRENTID --info | grep "Name:" | awk -F: '{print $NF}'` # post an update to twitter echo "$trname download was completed" | twidge update # Put "#" if you don't need this. # push update for pushover curl -s \ -F "token=$user-key" \ -F "user=$app-key" \ # -F "device=$device" \ # uncomment, if you want to send notification to a particular device. -F "title=Download Finished" \ -F "message=$trname download has completed." \ http://api.pushover.net/1/messages > /dev/null # The following codes works assuming One take advantage of gnome-power-manager by setting "black screen after 2/5/10/.. minitues ". # if monitor(Including laptop screen but EXCLUDING external monitor) is on, it will just force blank the screen, if not, it will shutdown/suspend or leave it as it is. # Modify it as per your requirement. STATUS=`xset -display $DISPLAY -q | grep 'Monitor'` #echo $STATUS if [ "$STATUS" == " Monitor is On" ] then notify-send "Downloads Complete" "turning off the screen now" xset dpms force off else notify-send "Downloads Complete" "$trname" # uncomment to shutdown the computer #dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.RequestShutdown # uncomment to suspend (on ubuntu) #dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend # uncomment to suspend (on Linux) (requires powermanagement-interface package) #pmi action suspend else echo "Torrent #$TORRENTID is not completed. Ignoring." fi done 

一个简单的脚本。

Thanx到Khurshid Alam和user98677我写了这个脚本,工作正常。 如果显示已打开(您正在工作),它会使传输退出并发送通知,如果不是计算机关闭。

  1. 安装

     sudo apt-get install transmission-cli libnotify-bin 

    在Ubuntu 16.04

     sudo apt install transmission-cli libnotify-bin 
  2. 传输>首选项>远程>选中允许远程访问确保HTTP端口为9091,并且仅允许这些IP地址为127.0.0.1(默认值)。
  3. 复制粘贴给定的脚本,保存为’shutdown.sh’并使其可执行。
  4. 传输>首选项>下载>检查’下载完成后调用脚本’,浏览到脚本。
  5. 系统设置>电源>屏幕亮度>在非活动时关闭屏幕>选择合理的时间。

     #!/bin/bash sleep 300s DISPLAY=:0.0 STATUS=$(xset -display $DISPLAY -q | grep 'Monitor') STATE=$(transmission-remote 127.0.0.1:9091 -tall --info | grep "^ State:" | grep "Down" | wc --lines) if [ "$STATUS" == " Monitor is On" ] && [ "$STATE" == "0" ] then notify-send "Downloads Complete" "Exiting transmisssion now" pkill transmission elif [ "$STATE" == "0" ] then #in Ubuntu 16,04 shutdown -h now #in older versions use the following #dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.RequestShutdown fi exit 0