我想使用 cron 每分钟运行这个 bash 代码。我把它保存到/root/activate.sh
#!/bin/bash
for file in /home/user/torrents/*.torrent
do
if [ "$file" != "/home/user/torrents/*.torrent" ]; then
echo [`date`] "$file" added to queue. >> /var/log/torrentwatch.log
/usr/bin/transmission-remote localhost:9091 -a "$file"
mv "$file" "$file".added
sleep 1
fi
done
Run Code Online (Sandbox Code Playgroud)
权限已设置-rwxrwxrwx 1 root root 278 May 27 01:27 activate.sh
然后crontab -e我把这个放在里面
* * * * * root sh /root/activate.sh
该脚本不执行,我收到此日志错误
May 27 01:40:02 media CRON[3556]: (root) CMD (root sh /root/activate.sh)
May 27 01:40:02 media CRON[3555]: (CRON) info (No MTA installed, discarding output)
---after a minute---
May 27 01:41:01 media CRON[3582]: (root) CMD (root sh /root/activate.sh)
May 27 01:41:01 media CRON[3581]: (CRON) info (No MTA installed, discarding output)
Run Code Online (Sandbox Code Playgroud)
首先,为什么不直接使用 Transmission 的 watch-dir 功能呢?
crontab条目是错误的,应该是* * * * * /root/activate.sh你用crontab -e. /etc/crontab并/etc/cron.d/*需要一个额外的用户名字段,但是您使用命令设置的用户特定的 crontabscrontab没有用户名字段;作业以运行 的用户身份运行crontab。
另外,由于此脚本对用户主目录中的文件进行操作,因此我将以该用户身份运行该作业。除了可能写入该日志文件之外,该脚本不需要 root 权限,但您可以更改该日志文件的所有权。
至于脚本,我会稍微修改一下:
#!/bin/bash
for file in ~user/torrents/*.torrent; do
[[ -f "$file" ]] || continue
transmission-remote -a "$file" && mv "$file" "$file.added" || continue
printf '[%s] %s added to queue\n' "$(date)" "$file"
sleep 1
done >> /var/log/torrentwatch.log
Run Code Online (Sandbox Code Playgroud)
最后,您应该避免为脚本添加扩展,尤其是.sh当脚本是 bash 脚本而不是 sh 脚本时不要使用。
| 归档时间: |
|
| 查看次数: |
29777 次 |
| 最近记录: |