评估变量失败(使用 Crontab)

发布于 2024-08-10 19:41:27 字数 901 浏览 2 评论 0原文

以下是我正在编写的用于记录 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 并检查输出。它有效。

  • egreptrcut 在 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 >>
    somefile
    and checking the output. It works.

  • egrep, tr, and cut 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚雾 2024-08-17 19:41:27

您的问题来自 mpstat。从命令提示符运行时,它会输出带有 AM/PM 的时间。当它由 cron 运行时则不会。正如ennukiller所说,这可能是一个环境问题。在我的系统上,echo $LANG 在命令提示符处给出“en_US.UTF-8”,但在 cron 中运行时没有任何结果。这个或其他一些环境变量正在影响 mpstat 输出时间的方式,因此当您 grep 查找“(AM|PM)”时,它找不到它。

顺便说一句,你为什么不直接这样做:

ldsys=$(/usr/bin/mpstat ... )

不分配给“cmd1”、evalecho和到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 by cron. As ennuikiller suggested, it's probably an environment problem. On my system echo $LANG gives "en_US.UTF-8" at the command prompt, but nothing when run in cron. This or some other environment variable is affecting the way mpstat outputs times so when you grep for "(AM|PM)" it doesn't find it.

By the way, why don't you just do:

ldsys=$(/usr/bin/mpstat ... )

without the assignment to "cmd1", the eval, the echo and the piping to sh?

z祗昰~ 2024-08-17 19:41:27

您遇到以下两个问题之一:

This is perhaps our number one complaint with cron. When you login to Unix, startup scripts setup your environment. You can see your environment with the commands "printenv" or "env". On the other hand, cron sets up only a sparse environment 

通常,任何程序的输出都会发送到 STDOUT(标准输出),并且通常会出现在某人的显示屏上。对于 cron 启动的作业,STDOUT 将定向到本地邮件子系统,这意味着 cron 作业生成的任何输出都将邮寄给拥有该作业的用户

You are encountering one of the two following issues:

This is perhaps our number one complaint with cron. When you login to Unix, startup scripts setup your environment. You can see your environment with the commands "printenv" or "env". On the other hand, cron sets up only a sparse environment 

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

北斗星光 2024-08-17 19:41:27

您是否尝试过消除eval

而不是

ldsys=`eval $cmd1`

尝试

ldsys=`echo "$cmd1" | /bin/sh`

ldsys="$(echo $cmd1 | /bin/sh)"

Have you tried eliminating the eval?

Instead of

ldsys=`eval $cmd1`

Try

ldsys=`echo "$cmd1" | /bin/sh`

or

ldsys="$(echo $cmd1 | /bin/sh)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文