Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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/
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?
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 ?).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.