加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 来自cronjob的“stdin:不是tty”

发布时间:2020-12-13 19:02:09 所属栏目:Linux 来源:网络整理
导读:每次执行特定的cronjob时,我都会收到以下邮件.当我直接调用它时,调用的脚本运行正常,甚至来自cron.所以我得到的消息不是一个实际的错误,因为脚本完全按照它应该做的去做. 这是cron.d条目: * * * * * root /bin/bash -l -c "/opt/get.sh /tmp/file" 和get.sh

每次执行特定的cronjob时,我都会收到以下邮件.当我直接调用它时,调用的脚本运行正常,甚至来自cron.所以我得到的消息不是一个实际的错误,因为脚本完全按照它应该做的去做.

这是cron.d条目:

* *     * * *     root   /bin/bash -l -c "/opt/get.sh > /tmp/file"

和get.sh脚本本身:

#!/bin/sh

#group and url
groups="foo"

url="https://somehost.test/get.php?groups=${groups}"

# encryption
pass='bar'
method='aes-256-xts'
pass=$(echo -n $pass | xxd -ps | sed 's/[[:xdigit:]]{2}/&/g')

encrypted=$(wget -qO- ${url})
decoded=$(echo -n $encrypted | awk -F '#' '{print $1}')
iv=$(echo $encrypted | awk -F '#' '{print $2}' |base64 --decode | xxd -ps | sed 's/[[:xdigit:]]{2}/&/g')

# base64 decode input and save to file
output=$(echo -n $decoded | base64 --decode | openssl enc -${method} -d -nosalt -nopad -K ${pass} -iv ${iv})

if [ ! -z "${output}" ]; then
        echo "${output}"
else
        echo "Error while getting information"
fi

当我没有使用bash -l语法时,脚本会在wget过程中挂起.所以我的猜测是它与wget有关并将输出放到stdout.但我不知道如何解决它.

最佳答案
你实际上有两个问题.

>为什么打印stdin:不是tty?

此警告消息由bash -l打印. -l( – login)选项要求bash启动登录shell,例如:通常在您输入密码时启动的那个.在这种情况下,bash期望它的stdin是一个真正的终端(例如,isatty(0)调用应该返回1),如果它由cron运行则不是真的 – 因此这个警告.

重现此警告的另一种简单方法是非常常见的,即通过ssh运行此命令:

$ssh user@example.com 'bash -l -c "echo test"'
Password:
stdin: is not a tty
test

这是因为当使用命令作为参数调用时,ssh不会分配终端(在这种情况下,应该使用-t选项来强制终端分配).

>为什么没有-l它不起作用?

正如@Cyrus在评论中正确陈述的那样,在启动时加载bash的文件列表取决于会话的类型.例如.对于登录shell,它将加载/ etc / profile,?/ .bash_profile,?/ .bash_login和?/ .profile(参见手动bash(1)中的INVOCATION),而对于非登录shell,它只会加载?/. bashrc中.您似乎只在为登录shell加载的一个文件中定义了您的http_proxy变量,但在?/ .bashrc中没有.你将它移动到?/ .wgetrc它是正确的,但你也可以在?/ .bashrc中定义它,它会工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读