'SCP not working in crontab but works on commandline

After much research, I couldn't find a solution but post this question.

I have a computer A and B both Ubuntu desktop. I want to copy file from A to B. Steps I followed.

1. ssh-keygen in computer A
2. Left password blank
3. Copied id_rsa.pub to computer B ~/.ssh/ from computer A
4. Renamed id_rsa.pub to authorized_keys in computer B
5. In computer A I did scp -i ~/.ssh/id_rsa -r /var/www/abc abc@ip:/home/abc/

If I do step 4 in commandline its working fine. But when I did same in crontab

22 10 * * * root scp -i ~/.ssh/id_rsa -r /var/www/abc abc@ip:/home/abc

Its doing nothing.



Solution 1:[1]

This is my solution. Made in Raspberry with Jessie OS.

Fix connection with the server with public key with no passphrase. You can find tutorials everywhere. The thing is do it as the same user that shall create the crontab. In my case I made the keys as PI (user on my Raspberry). Make sure you can login without password on your server.

Then I created my script that uploads all txt-files in a directory to the server every 5th minute. ex:

"#!/bin/bash scp /mnt/www/hus/*.txt [email protected]:/www/images/hustemp"

Save it as xxxxxxx.sh in your home dir and make it executable (chmod +x xxxxxxx.sh).

Then it? time to create the cronjob. I think you have to be in your home dir.Just run crontab -e (no sudo in front)and edit to what you want. In my case: */5 * * * * /home/pi/upload.sh

It works perfect!

Good Luck Anders

Solution 2:[2]

Why don't you try putting the the scp command in a bash script and put the bash script in the cron , also remember to put the shebang in your sh script as follows : #! /bin/bash (generally the path , confirm this by typing which bash in your shell). Also chmod a+x your sh script to make it executable and try the sh script from bash as ./script.sh and then put it in the crontab.

Why did the scp command not work in crontab? The following post does a good job explaining the different kinds of problems one faces with cron jobs - https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work

In your case it's an environment problem. Crontab's environment is different from that of bash's. Hope this helps.

Solution 3:[3]

In crontab, you have a mere execution of the command line without all the goodies of an interactive shell, that is a populated PATH variable, and all other bash tricks such as ~ interpretation (unsure for that last one).

So the rule is always use full paths in crontab:

22 10 * * * root /usr/bin/scp -i /home/username/.ssh/id_rsa -r /var/www/abc abc@ip:/home/abc

Solution 4:[4]

For those struggling with this issue, all the answers above didn't solve my problem. Actually, you have to do the scp once with the root account in the terminal, so that you get this message:

The authenticity of host 'XXXX (123.123.123.123)' can't be established.
ECDSA key fingerprint is SHA256:XXXXXXXXXXXxxxxXXXxxXXXxxXXxXxxXXX.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Then you type "yes" and your next crons will work like a charm.

Solution 5:[5]

  1. Create a shell script entering the scp command in the root
  2. Make the script executable
  3. Put the script in crontab
PATH=/usr/bin
32 18 * * * cd /root/ ; (time ./infra.sh)

Solution 6:[6]

Step 5 doesn't work,maybe Step 3 and 4 doesn't work well.

3. Copied id_rsa.pub to computer B ~/.ssh/ from computer A
4. Renamed id_rsa.pub to authorized_keys in computer B

You should use the command "ssh-copy-id" to copy .pub file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anders
Solution 2 Satyadev
Solution 3 Serge Ballesta
Solution 4 djcaesar9114
Solution 5
Solution 6 signjing