I’ve got a script in sh under Solaris 5.8 that isn’t working as expected and don’t really know why…
The script reads a list of URLs from a file, tests them with curl and writes the output to a log file:
#!/bin/sh
# Logs path
LOG_DIR=/somedir/logs
# URLs file path
URL_FILE=/somedir/url
# Actual date
DATE=`date +%Y%m%d%H%M`
# CURL
CURL=/somedir/bin/curl
test_url()
{
cat $URL_FILE | grep -i $1 | while read line
do
NAME=`echo $line | awk '{ print $1 }'`
URL=`echo $line | awk '{ print $2 }'`
TIME=`$CURL -s -o /dev/null -w %{time_total} $URL`
echo "$DATE $TIME" >> $LOG_DIR/${NAME}_${1}.log
done
}
test_url someurl
test_url someotherurl
The URL_FILE has this layout:
somename1 http://someurl/test
somename2 http://someotherurl/test
The script loads the URLs from the file and then uses curl to get the total time the URL takes to load, then prints the date and the time (in ms). The problem I find is that the variable TIME doesn’t work when called inside a crontab, but it does when called with the user itself:
# Output when called with the user ./script.sh
201202201018 0.035
# Output when called from crontab.
201202201019
If I redirect all output * * * * * /path/to/script/script.sh 1&2 > /tmp/output, the output file is blank.
Also I haven’t been able to see any output in /var/log/syslog about it. Any clue why TIME variable isn’t displaying correctly when called via crontab?
Things you should check out:
/path/to/script/script.sh 1&2 > /tmp/outputvalid in your cron? On my machine, it would run the script with argument “1” and try to find a program called “2” to run it. Failing to find it, it creates an empty output file. I think you’re looking for something like/path/to/script/script.sh >> /tmp/output 2>&1CURLto the full path of curl? Cron normally doesn’t have the same path information that you have.