Reputation: 257
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.
Upvotes: 6
Views: 20291
Reputation: 1
PATH=/usr/bin
32 18 * * * cd /root/ ; (time ./infra.sh)
Upvotes: 0
Reputation: 2137
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.
Upvotes: 0
Reputation: 29
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
Upvotes: 1
Reputation: 257
I have tried virtually every answer found related to the problem. The answer just came accidentally.
I typed username instead of root and it worked. I don't know how but it worked. Hope this will help people like me.
2 10 * * * root /usr/bin/scp -i /home/username/.ssh/id_rsa -r /var/www/abc abc@ip:/home/abc
2 10 * * * username /usr/bin/scp -i /home/username/.ssh/id_rsa -r /var/www/abc abc@ip:/home/abc
Upvotes: 3
Reputation: 148965
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
Upvotes: 0
Reputation: 98
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.
Upvotes: -1
Reputation: 643
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.
Upvotes: 0