LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   rsync doesn't work with crontab. (https://www.linuxquestions.org/questions/linux-software-2/rsync-doesnt-work-with-crontab-4175545095/)

quickbeard 06-11-2015 12:11 PM

rsync doesn't work with crontab.
 
Hi everyone,

I am trying to transfer data between 2 CentOS 6.6 computers through SSH without requiring password. I have successfully set up a keypair with no password and when I execute the below command on command-line interface, it totally worked:

rsync -av --delete -e ssh /backup/ root@xxx.xxx.20.209:/backup/

However, when I add this command to crontab, it wasn't executed:

42 11 * * * rsync -av --delete -e ssh /backup/ root@xxx.xxx.20.209:/backup/

It's pretty weird and I don't know what gone wrong. Plsease help me with this issue.

Thank you very much.

Habitual 06-11-2015 02:59 PM

What does /var/log/cron say about this?

Warning: obfuscate that IP, NOW. Re-edit your post and sub first 2 octets with xxx and yyy

eg: xxx.yyy.20.209

Edit: And welcome to LQ!

quickbeard 06-11-2015 03:05 PM

Hi Habitual,

It says:

Jun 11 14:57:46 mu-020200 crontab[1304]: (root) BEGIN EDIT (root)
Jun 11 14:58:15 mu-020200 crontab[1304]: (root) REPLACE (root)
Jun 11 14:58:15 mu-020200 crontab[1304]: (root) END EDIT (root)
Jun 11 14:59:01 mu-020200 crond[1211]: (root) RELOAD (/var/spool/cron/root)
Jun 11 14:59:02 mu-020200 CROND[1310]: (root) CMD (rsync -av --delete -e ssh /backup/ root@xxx.xxx.20.209:/backup/)


Seems like ther's no error. When I check /var/spool/cron/root, it says:

From root@mu-020200.dhcp.missouri.edu Thu Jun 11 14:59:22 2015
Return-Path: <root@mu-020200.dhcp.missouri.edu>
X-Original-To: root
Delivered-To: root@mu-020200.dhcp.missouri.edu
Received: by mu-020200.dhcp.missouri.edu (Postfix, from userid 0)
id C3E617FFA0; Thu, 11 Jun 2015 14:59:22 -0500 (CDT)
From: root@mu-020200.dhcp.missouri.edu (Cron Daemon)
To: root@mu-020200.dhcp.missouri.edu
Subject: Cron <root@mu-020200> rsync -av --delete -e ssh /backup/ root@128.206.20.209:/backup/
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20150611195922.C3E617FFA0@mu-020200.dhcp.missouri.edu>
Date: Thu, 11 Jun 2015 14:59:22 -0500 (CDT)

Permission denied, please try again.^M
Permission denied, please try again.^M
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).^M
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]


But when I typpe the command on CLI, there is no error like this, the file was sent succesfully. That's very weird.

Thank you for your help!

jailbait 06-11-2015 05:07 PM

Crontab has a different PATH than root or user. Try using the full path name when you execute rsync in crontab, for example:

42 11 * * * /usr/bin/rsync -av --delete -e ssh /backup/ root@xxx.xxx.20.209:/backup/

You can find out where rsync is located with:

which rsync

------------------------------
Steve Stites

Habitual 06-11-2015 05:33 PM

I have asked a moderator to obfuscate the IP address in your first post for you.

Tinkster 06-11-2015 06:00 PM

Quote:

Originally Posted by Habitual (Post 5375824)
I have asked a moderator to obfuscate the IP address in your first post for you.

I did, as a show of good will =}

In all honesty I don't think that (in this day) it makes a difference. Put a new machine w/ a public IP up and it
will be probed in under a minute anyway ...


Cheers,
Tink

quickbeard 06-12-2015 10:24 AM

Quote:

Originally Posted by Habitual (Post 5375824)
I have asked a moderator to obfuscate the IP address in your first post for you.

Quote:

Originally Posted by Tinkster (Post 5375830)
I did, as a show of good will =}

In all honesty I don't think that (in this day) it makes a difference. Put a new machine w/ a public IP up and it
will be probed in under a minute anyway ...


Cheers,
Tink

Thank you very much, Habitual and Tinkster!

Quote:

Originally Posted by jailbait (Post 5375816)
Crontab has a different PATH than root or user. Try using the full path name when you execute rsync in crontab, for example:

42 11 * * * /usr/bin/rsync -av --delete -e ssh /backup/ root@xxx.xxx.20.209:/backup/

You can find out where rsync is located with:

which rsync

------------------------------
Steve Stites

Hey Steve, I have tried your solution but it did not work as well. By the way, I think the problem is somehow crontab cannot make a connection through SSH because when I added "rsync -av --delete /backup1/ /backup2/" (local backup on 1 computer) to crontab, it worked. Thank you for your help but I still need more help in this case.

Once again, thank you guys for your assistance,

- Minh.

quickbeard 06-12-2015 02:37 PM

Hey guys,

I have figured out the problem. It seems that CLI and crontab run on different environments, so crontab need to be told where the private key is. I simply added the location of private key on the command:

42 11 * * * rsync -av --delete -e "ssh -i /root/.ssh/id_rsa" /backup/ root@xxx.xxx.20.209:/backup/

and it works now.

Thank you for all your help.

- Minh.

Habitual 06-12-2015 06:29 PM

Quote:

Originally Posted by Tinkster (Post 5375830)
I did, as a show of good will =}

Thanks.
Quote:

Originally Posted by Tinkster (Post 5375830)
In all honesty I don't think that (in this day) it makes a difference. Put a new machine w/ a public IP up and it will be probed in under a minute anyway ...

Yes, this is true. I was hoping to impart to the OP a "best practice" that is usual with posting IPs on public forums. No need to add to the fray!
We wouldn't advertise we have a home security system, now would we? ;)

who knows. xxx.yyy.20.209 (the original post IP) may not have even been "his" and if that's the case, he's smarter that I gave him credit for. No offense to quickbeard.

Have a Great Weekend!


All times are GMT -5. The time now is 07:08 PM.