Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i want to run this bash code every minute using cron. I saved it onto /root/


for file in /home/user/torrents/*.torrent

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

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

then inside my crontab -e i placed this

* * * * * root sh /root/

the script does not execute and i get this log error

May 27 01:40:02 media CRON[3556]: (root) CMD (root sh /root/
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/
May 27 01:41:01 media CRON[3581]: (CRON) info (No MTA installed, discarding output)
share|improve this question
up vote 4 down vote accepted

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

The crontab entry is wrong, it should be * * * * * /root/ 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:


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.

share|improve this answer
thank you sir!! watch-dir feature is already defined in settings.json and it doesn't seem to work out for me.. your code is perfect :) would you be so kind to explain what that code does in english? im still trying to learn about bash and yours is too advanced for me to understand :) thanks. – user1666411 May 27 '13 at 13:54
@user1666411 The main difference is that it checks that transmission-remote succeeds before renaming the file and printing the log message. One reason transmission's watch-dir may fail is when adding files over an external filesystem like NFS. transmission listens for inotify events in the watch-dir, but NFS doesn't trigger inotify events. – geirha May 29 '13 at 19:54

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/

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.