Shell脚本从命令行运行,而不是cron [英] Shell script runs from command line, not cron

查看:275
本文介绍了Shell脚本从命令行运行,而不是cron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,每天更新一次服务器的统计信息。

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.

下面是一个代码示例:

#!/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=

The result in the cron job is http://192.168.100.150/status.php?macaddr=&disk_size=&disk_used=

我我不知道是否是一些问题的变量,或者可能与awk尝试解析数据没有指定的终端大小等。

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.

任何帮助是赞赏。

推荐答案

当遇到这样的问题时,它几乎总是一个环境问题。

When you're running into problems like this it's almost always an environment issue.

将env的结果转储到文件并检查。您也可以使用

Dump the results of "env" to a file and inspect that. You can also run your script with top line of

#!/bin/sh -x

查看所有变量发生了什么。您可能想要使用包装器脚本,以便您可以重定向提供的分析输出。

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.

这篇关于Shell脚本从命令行运行,而不是cron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆