在 bash 脚本中回显变量时出现 SIGPIPE
我有一个 cronjob 在 debian 机器上运行。在脚本的某些时刻,我通过
HOSTNAME=$( hostname -s | tr A-Z a-z )
然后设置变量,然后将该值记录到系统日志(这样我可以看到一切都正常运行)
function log {
# just echo it
echo -n `date -u "+%s"`
echo -n " "
echo $1
/usr/bin/logger -t $0 -- $1
}
log "Hostname: ${HOSTNAME}"
但是,在此框中,我收到一个 SIGPIPE。这种情况在整个剧本中多次出现。我已经捕获了 SIGPIPE 来确认情况确实如此,但我想实际解决问题。
有人可以告诉我可能导致 SIGPIPE 的原因以及如何修复它吗?我尝试创建一个较小的测试脚本,但该脚本的反应方式不同。
I have a cronjob running on a debian box. At certain points through the script, I set variables via
HOSTNAME=$( hostname -s | tr A-Z a-z )
then later, I log that value to syslog (so I can see that everything is running properly)
function log {
# just echo it
echo -n `date -u "+%s"`
echo -n " "
echo $1
/usr/bin/logger -t $0 -- $1
}
log "Hostname: ${HOSTNAME}"
However, on this box, I receive a SIGPIPE. This happens many times throughout the script. I've trapped the SIGPIPE to confirm that this is the case, but would like to actually fix the problem instead.
Can someone tell me what might be causing the SIGPIPE, and how to fix it? I've tried creating a smaller test-script, but that script doesn't react the same way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测:在 cron 环境中,PATH 是有限的,这意味着 shell 可能无法找到 hostname 和/或 tr。首先,找出hostname和tr所在的位置并使用绝对路径,例如:
My guess: within the cron environment, the PATH is limited, which means the shell might not be able to find hostname and/or tr. First, find out where hostname and tr are located and use absolute path instead, for example: