0

I have a very simple cron job written in shell script, which scp a file from another server to local. e.g,

#!/bin/bash
scp $REMOTE:/$FILE ./

Of course, the server need a private key to login, which I have config file saving HostName, IdentityFile in (~/.ssh/id_rsa, a mod with 600).

However, it has "permission denied" issue when the cron running....

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/xxx>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=xxxx>
Message-Id: <20120118235702.33A7B16C03B5@xxx-xxxx-deskbox>
Date: Wed, 18 Jan 2012 18:57:02 -0500 (EST)

Permission denied (publickey).

I checked everywhere, it says the crontab will be running as the owner/user's environment.

Anyone have a solution for this? Thanks!

warren
  • 17,829
  • 23
  • 82
  • 134
  • 1
    This is not a Cron error, it is an SSH error. See http://serverfault.com/questions/39733/why-do-i-get-permission-denied-publickey-when-trying-to-ssh-from-local-ubunt – voretaq7 Jan 19 '12 at 00:27
  • The server won't accept your key. Check that you can log in manually. You can add key to command line `scp -i ~/.my_key_somewhere ...` – jkj Jan 19 '12 at 09:45
  • Thanks jkj. Yes, manually run the script is a success. I test it before I put in the crontab, thought it will be same.... I tried with -i in my script: `scp -i /home/xxxx/.ssh/id_rsa xxxx@$REMOTE:$FILE $FOLDER/ ` still no luck. Denied. – Guoqiang Huang Jan 19 '12 at 16:48

1 Answers1

1

Problem solved. There was a passphrase on my id_rsa key. Hence manually running won't require it as the phrase is in the session; while crontab need the passphrase. Delete the passphrase

ssh-keygen -p [-P old_passphrase] 

and enter empty for new passphrase; or check this page

Hope my answer can help some programmers several hours of debugging..