Shell 脚本从命令行运行,而不是 cron
我有一个脚本每天更新一次服务器的一些统计数据。从命令行运行时,脚本按预期工作,但从 cron 运行时,某些变量不会传递给curl。
以下是代码示例:
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
/bin/sh /etc/profile
MACADDR=$(ifconfig en0 | grep ether | awk '{print $2}')
DISKUSED=$(df / | awk '{print $3}' | tail -n1)
DISKSIZE=$(df / | awk '{print $2}' | tail -n1)
# HTTP GET PARAMS
GET_DELIM="&"
GET_MAC="macaddr"
GET_DS="disk_size"
GET_DU="disk_used"
# Put together the query
QUERY1=$GET_MAC=$MACADDR$GET_DELIM$GET_DS=$DISKSIZE$GET_DELIM$GET_DU=$DISK_USED
curl http://192.168.100.150/status.php?$QUERY1
cron 作业中的结果为 http ://192.168.100.150/status.php?macaddr=&disk_size=&disk_used=
我不确定变量是否有问题,或者可能是 awk 尝试解析没有终端大小的数据指定等。
感谢任何帮助。
I have a script that updates a server with some stats once per day. The script works as intended when running from command line, but when running from cron some of the variables are not passed to curl.
Here is an example of the code:
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
/bin/sh /etc/profile
MACADDR=$(ifconfig en0 | grep ether | awk '{print $2}')
DISKUSED=$(df / | awk '{print $3}' | tail -n1)
DISKSIZE=$(df / | awk '{print $2}' | tail -n1)
# HTTP GET PARAMS
GET_DELIM="&"
GET_MAC="macaddr"
GET_DS="disk_size"
GET_DU="disk_used"
# Put together the query
QUERY1=$GET_MAC=$MACADDR$GET_DELIM$GET_DS=$DISKSIZE$GET_DELIM$GET_DU=$DISK_USED
curl http://192.168.100.150/status.php?$QUERY1
The result in the cron job is http://192.168.100.150/status.php?macaddr=&disk_size=&disk_used=
I am not sure if it is some problem with the variables, or possibly with awk trying to parse data with no terminal size specified, etc.
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您遇到此类问题时,几乎总是环境问题。
将“env”的结果转储到文件并检查它。您还可以使用顶行运行脚本
来查看所有变量发生了什么。您可能想要使用包装器脚本,以便可以重定向提供的输出以供分析。
When you're running into problems like this it's almost always an environment issue.
Dump the results of "env" to a file and inspect that. You can also run your script with top line of
to see what's happening to all the variables. You might want to use a wrapper script so you can redirect the output this provides for analysis.
Mac 上的脚本
ifconfig
中的第一个命令位于/sbin/ifconfig
中。 cron 作业的默认 PATH 变量设置为:/usr/bin:/bin
这就是您的其余命令也可能失败的原因。最好在脚本顶部手动设置 PATH。像这样的东西:
Very first command in your script
ifconfig
is found in/sbin/ifconfig
on Mac. And the default PATH variable for cron jobs is set to:/usr/bin:/bin
That's the reason probably rest of your commands are also failing.It is better to set the PATH manually at the top of your script. Something like:
我在使用 crons 时遇到的一个问题是,你认为理所当然的变量并不存在。您认为理所当然的主要变量是路径变量。
回显从命令行运行时设置的路径,并将其放在脚本的顶部(或 crontab 的顶部)。
或者,指定每个命令的完整路径 - ifconfig、awk、grep 等。
我猜这会解决问题。
One problem I've run into with crons is that variables you take for granted do not exist. The main one you take for granted is the path variable.
Echo what you have set as your path when being run from the command line and put that in the top of your script (or in the top of the crontab).
Alternatively, specify the full path to each command - ifconfig, awk, grep, etc.
I would guess that will fix the problem.