为什么这个bash脚本在控制台上工作,但在作为cron作业运行时失败?

问题描述:

我有以下bash脚本,它在CLI中运行时没有问题,但在作为cron作业运行时失败。为什么这个bash脚本在控制台上工作,但在作为cron作业运行时失败?

#!/bin/bash 

cd /home/oompah/scripts/tests/ 
scp -P 12345 file1 [email protected]:~/uploads 

if scp -P 12345 [email protected]:/path/to/file2.dat local.dat >&/dev/null ; then 
    echo "INFO: transfer OK" ; 
else 
    echo "ERROR: transfer failed" ; 
fi 

该错误消息我得到(重定向到一个日志文件)当我运行它作为一个cron作业是:

ERROR: transfer failed 

该错误消息我在我的邮件收件箱中得到的是:

Permission denied (publickey). 
lost connection 

第一个scp(副本)也失败了(虽然我没有检查它)。有谁知道为什么会发生这种情况,以及我如何解决它?

BTW:我在Ubuntu LTS 10.0.4

[编辑]

运行此我添加了-i选项来SCP(脚本中第一个命令),并且还增加了调试(使用V选项)。以下是完整的调试跟踪:

Executing: program /usr/bin/ssh host 12.34.56.78, user oompah, command scp -v -t ~/uploads 
OpenSSH_5.3p1 Debian-3ubuntu6, OpenSSL 0.9.8k 25 Mar 2009 
debug1: Reading configuration data /etc/ssh/ssh_config 
debug1: Applying options for * 
debug1: Connecting to 12.34.56.78 [12.34.56.78] port 12345. 
debug1: Connection established. 
debug1: identity file /home/oompah/.ssh/id_rsa type 1 
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048 
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048 
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3p1 Debian-3ubuntu3 
debug1: match: OpenSSH_5.3p1 Debian-3ubuntu3 pat OpenSSH* 
debug1: Enabling compatibility mode for protocol 2.0 
debug1: Local version string SSH-2.0-OpenSSH_5.3p1 Debian-3ubuntu6 
debug1: SSH2_MSG_KEXINIT sent 
debug1: SSH2_MSG_KEXINIT received 
debug1: kex: server->client aes128-ctr hmac-md5 none 
debug1: kex: client->server aes128-ctr hmac-md5 none 
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent 
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP 
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent 
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY 
debug1: Host '[12.34.56.78]:12345' is known and matches the RSA host key. 
debug1: Found key in /home/oompah/.ssh/known_hosts:3 
debug1: ssh_rsa_verify: signature correct 
debug1: SSH2_MSG_NEWKEYS sent 
debug1: expecting SSH2_MSG_NEWKEYS 
debug1: SSH2_MSG_NEWKEYS received 
debug1: SSH2_MSG_SERVICE_REQUEST sent 
debug1: SSH2_MSG_SERVICE_ACCEPT received 
debug1: Authentications that can continue: publickey 
debug1: Next authentication method: publickey 
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 <unknown> 
debug1: read_passphrase: can't open /dev/tty: No such device or address 
debug1: No more authentication methods to try. 
Permission denied (publickey). 
lost connection 
Permission denied (publickey). 

希望这提供了更多的线索

+0

尝试chmod 644〜/。服务器上的ssh/authorized_keys – 2011-03-31 17:35:19

您可能正在运行ssh-agent或专门授权给您专用密钥。当您在cron中运行时,您没有会话,并且您没有任何与ssh-agentttys等会话一起读取密码的任何内容。

创建一个无密码的密钥并将公钥添加到~target/.ssh/authorized_keys下的目标帐户。然后,您可以使用刚创建的密钥scp来验证和复制文件。

仅供参考:您可能需要阅读关于command keys的ssh服务器手册页以及密钥访问和身份验证如何工作。

如果不更改用户帐户,这将是通常与环境问题。您可以通过运行env和/或设置并将输出重定向到一个文件来检查,比较cli和cron的结果。在这种情况下,在运行脚本之前,看起来cron正在执行set -x,因此第一个错误会导致脚本退出。

两种可能的解决方案。添加一个|| true到任何可能失败而没有任何问题的命令。或者,您可以在脚本顶部执行set +x以恢复您在命令行上的行为。


编辑:从头开始。我虽然你的剧本在第一个scp上死了,直到我重读你的问题。这很可能是您的环境问题。将env >/path/env.out放在脚本的顶部,并比较您的cli和cron结果。


编辑:另一个想法是,你是否加密主目录,如果是,你登录时,这个cron脚本运行?如果您未使用加密目录登录,则无法运行该目录。由于这个原因,我只在桌面上加密主目录,我将永远登录。

+0

如果您的问题是encryptfs,请参阅以下主题:http://ubuntuforums.org/showthread.php?t=1258294 – BMitch 2011-03-31 13:56:10