我有一个用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
任何想法都非常感谢.
解决方法
尝试将-t -t添加到SSH连接选项中.这将;强制分配伪终端.
Multiple
-t
options force tty allocation,even ifssh
has no local tty.