Crontab issue with specific days execution

We have a cron job under a specific user’s crontab which should be running only on working week days. Job was running all days before we changed it.

After we changed in the following line the days statement from ‘*’ to ‘1-5’, testscript.sh is still running all days and the weekend that is not supposed to.

00 21 * * 1-5 /usr/local/bin/test_script.sh >> /var/log/userdirectory/test_script.log 2>&1

Any ideas why?

Answer

Well, the most likely case is that something went wrong when you were updating crontab. If you can’t track down the specific issue, there’s an easy workaround: in test_script.sh, add a couple of lines to quit if it’s not one of the correct days for running the script. Something like:

#quit if we're doing this on the wrong day of the week
if date | grep Sun &> /dev/null
then
exit 0
fi
if date | grep Sat &> /dev/null
then
exit 0
fi

(yes, I know there’s better ways to do those statements; this is just a way to do it that is really easy to understand and see what it’s doing)

Attribution
Source : Link , Question Author : fotag , Answer Author : Robert

Leave a Comment