bash: using scp in cron job fails, but runs succesfully when run from command line

Solution 1:

My guess:

You have a password-protected SSH keypair, which is automatically loaded by GNOME Keyring when you login. However, cron does not have access to the keyring, and ssh cannot ask for a password either (due to lack of a tty).

To quote the ssh log you added:

debug1: Offering public key: /home/oompah/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: PEM_read_PrivateKey failed
debug1: read PEM private key done: type
debug1: read_passphrase: can't open /dev/tty: No such device or address

Solution 2:

It sounds like scp isn't picking up your public/private key pair from your ~/.ssh directory.

Try adding

HOME=/home/oompah

into the top of your crontab file (it should already be setting that anyway automatically)

You could also try adding

echo "DEBUG: My home dir is $HOME"

into your script to make sure it's getting the right value.

Another option is to specify the -i parameter to scp to force a specific key pair to use:

scp -i /home/oompah/.ssh/id_rsa ...

for example.

Solution 3:

What user is cron running as? It looks like that user doesn't have access to your public key.