Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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)
share|improve this question
add comment

2 Answers

up vote 2 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/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.

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
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

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.