Anyone have any ideas? I'm at a loss...
Why would a script make grep matches from the shell, but not from crontab? I thought it might be a permissions issue, so i set both the script and the file "important_dates" to 777. The tempfile is created and deleted within the script, so I'm not sure how it's permissions are set. Also, I fully qualified the path to every file involved.
The below script is designed to read a particular file (important_dates) grep for particular lines based on the date. Then email me the results.
It finds matches and works perfectly from the command prompt when I execute it with
sh test.sh
but it does not find any matches when i run it from cron
*/2 * * * * /path/to/test.sh 1>/path/to/results.out 2>&1
here is the script:
#!/bin/bash
TODAY=`date +%j`
DATETODAY=`date "+%A %B %d"`
FIRST=`expr $TODAY + 5`
if [ $FIRST -gt 366 ] ; then
FIRST=`expr $FIRST - 366`
fi
SECOND=`expr $FIRST + 1`
if [ $SECOND -gt 366 ] ; then
SECOND=`expr $SECOND - 366`
fi
THIRD=`expr $SECOND + 1`
if [ $THIRD -gt 366 ] ; then
THIRD=`expr $THIRD - 366`
fi
FOURTH=`expr $THIRD + 1`
if [ $FOURTH -gt 366 ] ; then
FOURTH=`expr $FOURTH - 366`
fi
FIFTH=`expr $FOURTH + 1`
if [ $FIFTH -gt 366 ] ; then
FIFTH=`expr $FIFTH - 366`
fi
SIXTH=`expr $FIFTH + 1`
if [ $SIXTH -gt 366 ] ; then
SIXTH=`expr $SIXTH - 366`
fi
SEVENTH=`expr $SIXTH + 1`
if [ $SEVENTH -gt 366 ] ; then
SEVENTH=`expr $SEVENTH - 366`
fi
date
echo "Today is $DATETODAY" > /path/to/tempfile
echo >> /path/to/tempfile
chmod 777 /path/to/tempfile
echo "$TODAY, $FIRST, $SECOND, $THIRD, $FOURTH, $FIFTH, $SIXTH, $SEVENTH" >> /path/to/tempfile
grep -w "\^^$FIRST" /path/to/important_dates | cut -d " " -f 2-20 >> /path/to/tempfile
grep -w "\^^$SECOND" /path/to/important_dates | cut -d " " -f 2-20 >> /path/to/tempfile
grep -w "\^^$THIRD" /path/to/important_dates | cut -d " " -f 2-20 >> /path/to/tempfile
grep -w "\^^$FOURTH" /path/to/important_dates | cut -d " " -f 2-20 >> /path/to/tempfile
grep -w "\^^$FIFTH" /path/to/important_dates | cut -d " " -f 2-20 >> /path/to/tempfile
grep -w "\^^$SIXTH" /path/to/important_dates | cut -d " " -f 2-20 >> /path/to/tempfile
grep -w "\^^$SEVENTH" /path/to/important_dates | cut -d " " -f 2-20 >> /path/to/tempfile
mail user@domain.com < /path/to/tempfile -s "Upcoming Events"
rm -f /path/to/tempfile