linux 在cli运行程序,linux – ssh在从cron bash脚本运行时无法执行远程命令 – 从CLI工作...

我有一个用bash编写的脚本,它来自cron.它做的第一件事就是SSH到远程主机并检索目录中的文件列表.从命令行运行时,一切正常,但不是来自cron.

脚本的部分最初看起来像这样:

FILES=$($SSH_BINARY -i $SSH_KEY $SSH_USER@$REMOTE_HOST "ls $REMOTE_DIRECTORY")

echo "Got files = $FILES"

我在该行上方添加了一个echo语句(如下所示),以证明它不是路径或变量问题:

echo "$SSH_BINARY -i $SSH_KEY $SSH_USER@$REMOTE_HOST \"ls $REMOTE_DIRECTORY\""

如果我获取结果输出行并以相同的用户cron(root)运行它,它可以正常工作.

认为它可能与赋值给变量有关,我修改了FILES =行来读取(因此,将输出直接放入我的last_run_output文件中):

$SSH_BINARY -vv -i $SSH_KEY $SSH_USER@$REMOTE_HOST "ls $REMOTE_DIRECTORY"

cron条目如下所示:

34 * * * * /root/path/to/my/script/get_logs.sh > /root/path/to/last_run_output 2>&1

所以,除了PATH,变量赋值和权限问题之外,我开始在ssh中使用调试标志.我从命令行运行一次,然后从cron运行,并比较输出.以下是差异的一些亮点:

– 方是不成功的尝试,方是成功的尝试,在cron之外运行.

@@ -87,9 +77,7 @@

debug1: Remote: X11 forwarding disabled.

debug1: Remote: Forced command: /home/sshacs/acssshsink netstorageuser

debug1: Authentication succeeded (publickey).

-debug2: fd 4 setting O_NONBLOCK

debug2: fd 5 setting O_NONBLOCK

-debug2: fd 6 setting O_NONBLOCK

debug1: channel 0: new [client-session]

debug2: channel 0: send open

debug1: Entering interactive session.

我无法解释为什么在从cron运行时在debug2中提到这些额外的文件描述符,但它似乎是相关的(通知读取< = 0 rfd 4 len 0 line below):

@@ -100,20 +88,672 @@

debug2: callback done

debug2: channel 0: open confirm rwindow 0 rmax 32768

debug2: channel 0: rcvd adjust 131072

-debug2: channel 0: read<=0 rfd 4 len 0

-debug2: channel 0: read Failed

-debug2: channel 0: close_read

-debug2: channel 0: input open -> drain

-debug2: channel 0: ibuf empty

-debug2: channel 0: send eof

-debug2: channel 0: input drain -> closed

+ [[ Very large output of the ls command ]]

+debug1: client_input_channel_req: channel 0 rtype exit-status reply 0

debug2: channel 0: rcvd eof

debug2: channel 0: output open -> drain

debug2: channel 0: obuf empty

debug2: channel 0: close_write

debug2: channel 0: output drain -> closed

-debug1: client_input_channel_req: channel 0 rtype exit-status reply 0

debug2: channel 0: rcvd close

+debug2: channel 0: close_read

+debug2: channel 0: input open -> closed

debug2: channel 0: almost dead

debug2: channel 0: gc: notify user

debug2: channel 0: gc: user detached

任何想法都非常感谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值