executing a shell every minute using cron

i want to run this bash code every minute using cron. I saved it onto /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

permission is set -rwxrwxrwx 1 root root 278 May 27 01:27 activate.sh

then inside my crontab -e i placed this

* * * * * root sh /root/activate.sh

the script does not execute and i get this log error

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)

2 Answers

First off, why aren't you just using transmission's watch-dir feature?

The crontab entry is wrong, it should be * * * * * /root/activate.sh when you add it with crontab -e. /etc/crontab and /etc/cron.d/* takes an extra username field, but the user specific crontabs which you set with the crontab command, does not have a username field; the jobs are run as the user that ran crontab.

Also, since this script operates on files in user's homedir, I would've run the job as that user. There's nothing about that script that requires root permissions, apart from maybe writing to that log file, but you can just change ownership of that logfile instead.

As for the script, I'd modify it a bit:

#!/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

Lastly, you should avoid adding extensions for scripts, and especially not use .sh when the script is a bash script, not an sh script.

2

When using crontab -e you can't specify a username. The format is:

m h dom mon dow command

So you should put this in crontab -e:

* * * * * /root/activate.sh

You don't have to use sh because the file has execute permission and a shebang line (#!/bin/bash).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like