评估变量失败(使用 Crontab)
以下是我正在编写的用于记录 CPU 负载的 bash 脚本的片段:
#!/bin/bash
# ... irrelevant nonsense ...
cmd1="/usr/bin/mpstat -P ALL | egrep '(AM|PM)([[:space:]]+)(0)' | tr -s ' ' | cut -d' ' -f4"
ldsys="$(echo $cmd1 | /bin/sh)"
# ... irrelevant nonsense ...
当从控制台以常规方式执行脚本时,$ldsys
已正确设置。它是金色的。 问题如下:使用 crontab 执行时,$ldsys
为空。
在过去的三个小时里,我已经尝试了数百万件事,试图让这个东西发挥作用......但我找不到任何东西。有人有什么想法吗?
注意:
/usr/bin/mpstat
可以由以下命令执行 计划任务。我通过添加一个每分钟触发一次的虚假任务进行了测试:/usr/bin/mpstat -P ALL >>> somefile
并检查输出。它有效。egrep
、tr
和cut
在 cron 下都可以正常工作。我认为这确实与 eval 赋值约定有关...但我不知道为什么这会成为一个问题,因为它是一个相对基本的构造...在尝试 Adam 的建议之后,我现在不知道该怎么想...
编辑:删除了eval
用法...仍然没有骰子。
Here's a snippet of a bash script I'm writing to log CPU loads:
#!/bin/bash
# ... irrelevant nonsense ...
cmd1="/usr/bin/mpstat -P ALL | egrep '(AM|PM)([[:space:]]+)(0)' | tr -s ' ' | cut -d' ' -f4"
ldsys="$(echo $cmd1 | /bin/sh)"
# ... irrelevant nonsense ...
$ldsys
is set properly when the script is executed conventionally from the console. It's golden. Here's the issue: when executed with crontab, $ldsys
is empty.
I've been trying millions of things for the last three hours to try to get this thing work... but I can't find anything. Does anyone have any ideas?
Notes:
/usr/bin/mpstat
can be executed by
cron. I tested by adding a bogus task to fire every minute:/usr/bin/mpstat -P ALL >>
and checking the output. It works.
somefileegrep
,tr
, andcut
all function fine under cron.I'm thinking it really has to do with the eval assignment convention... but I don't know why that would be an issue considering it's a relatively-fundamental construct...After trying Adam's suggestion, I now have no idea what to think...
Edit: stripped out eval
usage... still no dice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题来自
mpstat
。从命令提示符运行时,它会输出带有 AM/PM 的时间。当它由cron
运行时则不会。正如ennukiller所说,这可能是一个环境问题。在我的系统上,echo $LANG
在命令提示符处给出“en_US.UTF-8”,但在cron
中运行时没有任何结果。这个或其他一些环境变量正在影响mpstat
输出时间的方式,因此当您grep
查找“(AM|PM)”时,它找不到它。顺便说一句,你为什么不直接这样做:
不分配给“cmd1”、
eval
、echo
和到sh
的管道>?Your problem is coming from
mpstat
. When it's run from the command prompt, it outputs the time with AM/PM. It doesn't when it's run bycron
. As ennuikiller suggested, it's probably an environment problem. On my systemecho $LANG
gives "en_US.UTF-8" at the command prompt, but nothing when run incron
. This or some other environment variable is affecting the waympstat
outputs times so when yougrep
for "(AM|PM)" it doesn't find it.By the way, why don't you just do:
without the assignment to "cmd1", the
eval
, theecho
and the piping tosh
?您遇到以下两个问题之一:
通常,任何程序的输出都会发送到 STDOUT(标准输出),并且通常会出现在某人的显示屏上。对于 cron 启动的作业,STDOUT 将定向到本地邮件子系统,这意味着 cron 作业生成的任何输出都将邮寄给拥有该作业的用户
You are encountering one of the two following issues:
Normally the output of any program is sent to STDOUT (standard out) and usually winds up on someone's display screen. For jobs started by cron, STDOUT will be directed to the local mail subsystem, which means any output generated by a cron job will be mailed to the user owning the job
您是否尝试过消除
eval
?而不是
尝试
或
Have you tried eliminating the
eval
?Instead of
Try
or