0

I'm trying to run rsync-books with a crontab. Typing crontab -e outputs:

55 12 * * * diegoaguilar /storage/bin/rsync-books

Where /storage/bin/rsync-books looks like this:

if [ -d "/media/Beagle/books" ]; then
  rsync -rP --delete --verbose /storage/Copy/Books/ /media/Beagle/books >> ~/rsync-books.log
fi

Just to confirm, this script has got executable permissions. I tried waiting at that time the command, when /media/Beagle/books existed and it neither rsync anyhing or the log file was created.

Is there something I'm missing?

4
  • do you run crontab -e as user diegoaguilar?
    – blissini
    Commented Nov 9, 2015 at 20:27
  • Yep @blissini I did Commented Nov 9, 2015 at 20:28
  • 3
    so there is no need to specify the user. What happens if you create the entry: * * * * * touch /home/diegoaguilar/testfile, Does testfile get created in your home directory?
    – blissini
    Commented Nov 9, 2015 at 20:32
  • crontab -e and remove 'diegoaguilar'
    – Jeff Schaller
    Commented Nov 9, 2015 at 20:41

1 Answer 1

3

Cron runs commands without an environment so there's no PATH variable set. Because of this you need to specify the full path to rsync in your script.

if [ -d "/media/Beagle/books" ]; then
  /usr/bin/rsync -rP --delete --verbose /storage/Copy/Books/ /media/Beagle/books >> ~/rsync-books.log
fi

Also, if you're running crontab -e don't include the username in the crontab entry. Your crontab should look like

55 12 * * * /storage/bin/rsync-books

EDIT: cron runs in a non-interactive shell so the environment (and PATH) may be different from what you expect. It's always best to specify full paths in any script that will be run from cron.

6
  • not true on my test system (Debian 8.2).
    – blissini
    Commented Nov 9, 2015 at 20:26
  • @blissini What's not true? Does cron run with an environment or is rsync not located at /usr/bin/rsync?
    – David King
    Commented Nov 9, 2015 at 20:28
  • my cron entry: * * * * * touch /home/blissini/testfile works as expected. Doesn't cron run commands as my user, if I create cronjobs via crontab -e?
    – blissini
    Commented Nov 9, 2015 at 20:29
  • cron does indeed have an environment and sets PATH (by default). See unix.stackexchange.com/questions/56491/…
    – Jeff Schaller
    Commented Nov 9, 2015 at 20:41
  • Removing the user solved it. Now I should ask, what's correct way to create and register a cronjob Commented Nov 9, 2015 at 20:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .