LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   rsync runs fine in terminal but not in cron (https://www.linuxquestions.org/questions/linux-newbie-8/rsync-runs-fine-in-terminal-but-not-in-cron-4175578275/)

Entropy1024 04-25-2016 08:24 AM

rsync runs fine in terminal but not in cron
 
I have run an rsync command from the terminal and it works fine. When I try it in cron or even sudo cron it fails to work.
I have the machine set up with SSH keys so no password is required.

This is how I have entered it onto cron:
*/5 * * * * rsync -av -e "ssh -p 2280" --delete /home/tim/Documents/pi-2-black tim@192.168.0.80:/home/tim/Documents/

Where am I going wrong?

Many thanks
Tim

thesnow 04-25-2016 08:49 AM

Try putting the full path to the rsync command in there, e.g., /usr/bin/rsync . . .

sarnobat 04-25-2016 01:23 PM

1) I've run into this kind of cron vs terminal issue myself and the first thing to do is pipe both stdin and stderr to the same log file.

So do something like this:

*/5 * * * * rsync -av -e "ssh -p 2280" --delete /home/tim/Documents/pi-2-black tim@192.168.0.80:/home/tim/Documents/ 2>&1 | tee -a ~/rsync_documents.log

2) Use rsync's --progress and --verbose options to see more of what, if anything, did get transferred

Turbocapitalist 04-25-2016 01:36 PM

Quote:

Originally Posted by Entropy1024 (Post 5536197)
I have run an rsync command from the terminal and it works fine. When I try it in cron or even sudo cron it fails to work.
I have the machine set up with SSH keys so no password is required.

The key probably should be identified explicitly using -i

Also, are the environment variables SSH_AGENT_PID and SSH_AUTH_SOCK being properly filled in cron?

xamaco 04-26-2016 03:20 AM

There are several cron daemons. Try to identify yours (dixie cron, vixie cron, fcron, ...). The one I have is dixie cron and it's quite restrictive: It uses the shell /bin/sh, which on my distro is not bash. It uses only four environment variables: USER, LOGNAME, HOME, and SHELL...

To avoid problems, simply put your rsync command in a shell script with a nice shebang at the beginning (#!/bin/bash for me) and put the full path to the script in your cron job. This way, your rsync command will run in the same environment as it does in your terminal. Be careful though at the ID under which the job will run (your id or root ?).

Turbocapitalist 04-26-2016 04:10 AM

Quote:

Originally Posted by xamaco (Post 5536631)
This way, your rsync command will run in the same environment as it does in your terminal.

Close. Not all the variables will be set, notably SSH_AGENT_PID and SSH_AUTH_SOCK. They are provided by ssh-agent which many (most?) desktops set up for you. Though Entropy1024 did not say anything about using an agent for the SSH keys, that is one possible explanation for the symptoms. The desktop hides these pieces from the user, unless they look. The SSH client looks for the two variables to find the agent. In the terminal they will be there. In cron, a separate agent needs to be running independent of cron and the desktop, the key(s) loaded into it, and the script's use of ssh pointed at that agent by means of the variables.


All times are GMT -5. The time now is 09:58 AM.