1

I'm using rsync to backup directories on my local machine to a remote server. I have SSH key-based access set up and working for my user, so when I run the command:

rsync --update -razvP --progress ~/Projects/ ~/Documents/ [email protected]:backup/

Everything works as expected. So now I want to run that every day, so I pit it into a Bash script to be run via crontab:

~/backup1.sh

#!/usr/bin/env bash
rsync --update -razvP --progress ~/Projects/ ~/Documents/ [email protected]:backup/


$ ll ~/backup1.sh 
-rwxr-xr-x  1 my-user  admin   112B Nov 29 20:00 backup1.sh

$ crontab -e
30 15 * * * sh ~/backup1.sh

Unfortunately, when the cron job runs, it produces the following error:

Permission denied, please try again.
[email protected]: Permission denied (publickey,password).

I'm guessing crontab runs as its own user and thus is not finding my public key. IS there any way to tell either crontab or rsync to run as my user so that my public key is sent and authentication is successful?

4
  • If you feed a Search Engine such as the Mighty "Norton Safe Search" Search Engine the "Running cron jobs as a certain user" Character String, then you get as the First Result a Question that already has tackled this Issue. For instance, try applying the crontab -u my-user -e Command.
    – user1018743
    Dec 4, 2019 at 18:09
  • crontab normally operates on the crontab file belonging to user you run it as. So if you run it as my-user, it'll edit my-user's crontab file, and the listed jobs will run as my-user. Does access to the ssh keys depend on having an ssh-agent running? Dec 4, 2019 at 19:25
  • I'm on Mac so the ssh agent runs continuously. @DOBRESCU_Mihai's suggestion seems to have done the trick. No more Permission denied error.
    – Daveh0
    Dec 5, 2019 at 21:00
  • What can we say, @Daveh0? The Unix Family of Operating Systems is indeed ancient. Apparently, you are using one of the MacOS Flavors. If you feel like, you could edit the Body of your Question in order to contain the Output of the uname -a Command. The crontab -u my-user -e Command was just one of the suggestions that appears on the other Question. It seems that you have applied it. It also appears that the -u my-user Switch is self-implied and therefore redundant. Since it has solved your Issue, then it must be one of the particularities of your own Version of the Operating System.
    – user1018743
    Dec 6, 2019 at 16:43

0

You must log in to answer this question.

Browse other questions tagged .