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

发布于 2024-10-28 08:28:08 字数 2993 浏览 4 评论 0原文

我有以下 bash 脚本,它在 CLI 上运行没有问题,但作为 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 (副本)也失败了(尽管我没有检查它) 。有谁知道为什么会发生这种情况,以及我该如何解决它?

顺便说一句:我在 Ubuntu 10.0.4 LTS 上运行它

[编辑]

我向 scp 添加了 -i 选项(脚本中的第一个命令),并且还添加了调试(使用 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).

希望这提供更多线索

I have the following bash script, which runs without problems at the CLI, but fails when run as a cron job.

#!/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

The error message I get (redirected to a log file) when I run it as a cron job is:

ERROR: transfer failed

The error message I get in my mail inbox is:

Permission denied (publickey).
lost connection

The first scp (copy) fails as well (although I am not checking it). Does anyone know why this is happening, and how I may fix it?.

BTW: I am running this on Ubuntu 10.0.4 LTS

[Edit]

I added the -i option to scp (first command in script), and also added debugging (using the v option). Here is the full debug trace:

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).

Hopefully this provides more clues

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多彩岁月 2024-11-04 08:28:08

您可能正在使用 ssh-agent 运行或已专门对您的私钥进行了身份验证。当您在 cron 中运行时,您没有会话,并且没有任何与会话相关的内容,如 ssh-agentttys从中读取密码。

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

仅供参考:您可能需要阅读有关命令密钥的 ssh 服务器手册页以及密钥访问和身份验证的工作原理。

You are probably running with an ssh-agent or have auth'ed you private key specifically. When you run in cron you do not have a session, and you do not have anything that goes along with sessions like ssh-agent or ttys to read a password from.

Create a password-less key and add the public key to the target account under ~target/.ssh/authorized_keys. You will then be able to use the key you just created with scp to auth and copy the file.

FYI: You may want to read the ssh server man page on command keys and how key access and authentication work.

Spring初心 2024-11-04 08:28:08

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

两种可能的解决方案。添加<代码>|| true 对于任何可能失败而不会出现任何问题的命令。或者,您可以在脚本顶部执行 set +x 以恢复为命令行上的行为。


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


编辑:另一个想法是,您是否正在加密主目录,如果是,那么当此 cron 脚本运行时您是否登录?如果您未使用加密目录登录,则将无法运行此程序。因此,我只加密我始终登录的桌面上的主目录。

If you don't change user accounts, this would is usually be a problem with the environment. You can check that by running env and/or set and redirecting the output to a file, comparing the results from the cli and cron. In this case, it looks like cron is doing a set -x before running your script so the first error causes the script to exit.

Two possible solutions. Add a || true to any command that can fail without any issues. Or you can do a set +x at the top of your script to revert to the behavior you have on the command line.


Edit: scratch that. I though your script was dying on the first scp until I reread your question. It's most likely a problem with your environment. Put an env >/path/env.out at the top of your script and compare your cli vs cron results.


Edit: one other idea, are you encrypting home directories, and if so, are you logged in when this cron script runs? If you aren't logged in with the directory encrypted, you won't be able to run this. For this reason, I only encrypt home directories on desktops that I'll always be logged in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文