I am looking for some knowledge to help me understand why my use of cron fails when using rsync to transfer files from remote server to local path.
My aim is to have a scheduled cron job run at a set time and successfully execute my transfer script
From numerous Google searches I put together the following rsync command:
rsync --protect-args --remove-source-files --chmod=Du=rw,Dgo=rw,Fu=rw,Fog=r -apvzrc pi@192.168.0.10:/media/pi/USBSTORAGE32/ /media/Data_1/"James' Files"/
I have set up the remote server so it doesn't ask for a password by using ssh-keygen rsa to generate a public key which is stored in an authorised_keys file. I log into the 'local' machine using SSH and type the command to 'pull' the files from the remote server to the local machine.
Once I'd tested the rsync command above I thought I had it nailed so popped this into a script which then added to cron via crontab. My added line looks like this
55 * * * * sudo /home/jlivin25/bin/myscripts/sync.sh > /var/log/james/cronsync.log 2>&1
And my script looks like this:
#!/usr/bin/env bash
rsync --protect-args --remove-source-files --chmod=Du=rw,Dgo=rw,Fu=rw,Fog=r -apvzrc pi@192.168.0.10:/media/pi/USBSTORAGE32/ /media/Data_1/"James' Files"/
The scheduled task fails to run as expected, cron trys to run the script but the following error message is logged in my log file:
sudo: no tty present and no askpass program specified
And its at this point that I'm stuck, because by 'googling' the above message I get lots of differing reasons that what I have done is not working. I'm not sure where to chase the error to, is the issue with my use of cron, ssh or rsync.
Any help would be appreciated.
littlejeem
So very likely cron
is a daemon that is executed by user cron
in your system. When the cron
daemon decides to run a cronjob, it does so as the cron
user. But very likely the cron
user is not allowed to sudo in your system, so this accounts for the mentioned sudo error (as sudo usually asks for a password on forbidden uses, but in this case there is no terminal or password request program for cron
user to acquire one).
A way of having this confirmed is to run something like env | sort >/tmp/environment.log
env | sort >/tmp/environment.log
in the start of your script and check for key variables (such as USER
and HOME
) after execution. You can even compare the environment with the one when your script is executed manually.
I believe an approach here is obviously to configure sudo to allow cron
to run your script as the "correct" user. Another approach would be to make your script source (or replicate) some of the environment of the "correct" user, so sudo would not be required. I cannot tell which one is more effective to your goals.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.