Shell script is run from the command line, not cron

I have a script that updates the server with some statistics once a day. The script works as intended when launched from the command line, but when launched from cron, some variables are not passed to curl.

Here is a sample 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 of the cron job is http://192.168.100.150/status.php?macaddr=&disk_size=&disk_used=

I'm not sure if this is a problem with variables or possibly awk trying to parse data without a specified terminal size, etc.

Any help is appreciated.

+2
source share
3 answers

When you encounter such problems, it is almost always an environmental problem.

"env" . script

#!/bin/sh -x

, . , script, , .

+7

script ifconfig /sbin/ifconfig Mac. PATH cron : /usr/bin:/bin , , , .

PATH script. - :

export PATH=$PATH:/sbin
+4

, , , , , . , , - .

Change what you set as your path when starting from the command line and put it at the top of your script (or at the top of crontab).

Alternatively, specify the full path to each command - ifconfig, awk, grep, etc.

I would suggest that this fix the problem.

+2
source

All Articles