bash script conditional statement behaving differently when run under cron

On Ubuntu 10.04.  This certain piece of code:

if [ $(date +%w) = 0 ]; then
   #  code to run if it's a Sunday
  #  code to run to do regular stuff on other days

Open in new window

works great when the script is run from command line, but never seems to run the "Sunday code" when triggered via cron - it always runs the "regular" code.  I've tried many different things, such as putting the full path for the `date` command, as well as trying other options such as %u (7 for Sunday) and storing the output of `date +%w` to a variable first.  Any idea what's going on?  Thanks!
LVL 24
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

try to set the shell to use for running the code plus let run in logging mode and redirect output to a file and look at the output

for example add line like one below to your script

#!/usr/bin/ksh -x
Gerwin Jansen, EE MVETopic Advisor Commented:
When running from cron I'd use cron's feature to run on Sundays, like this:
00 08 * * 0 /full/path/to/your/script

Open in new window

The single 0 above means to run on Sunday, * * means on every day and 00 08 means at eight in the morning.

When running a script from cron, be sure to set required paths and variables in the script itself as cron has no knowlegde of them by itself.

Create/edit the crontab entry using crontab -e and add #!/bin/bash as a first line in your script.
johanntagleAuthor Commented:
That was just a snippet of the part that was troubling me.  I do have "#!/bin/bash" at top of my script.  I'm not sure what would be the counterpart of ksh -x in bash?

While I do have the option of running a separate script for Sundays, I would really like to know why this isn't working.  As I said, I already tried putting the full paths to the commands I ran.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Do you have the date command only in your script or also in crontab?

If it's in crontab then please note that the percent (%) signs have a special meaning there (start of stdin).

So inside a crontab it's mandatory escaping them using a backslash:

if [ $(date "+\%w") -eq 0 ]; then

If it's not in crontab - some remarks:

- I'd always use "-eq" for numerical comparisons instead of "=" (see above).
- I'd put double quotes around the format string: "+%w" (see above).
- Could it be that "/bin/bash" is not the same shell as the one you're using from command line?
I'm aware that any bash should well understand $(...), but you could try the backtick notation ` ... ` instead, just to be sure:

if [ `date "+%w"` -eq 0 ]; then

Btw. bash accepts "-xv" just the same way ksh does.

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
As you are running from cron it might not know where `date` is so you can specify it as the full path, if date is in /usr/bin for example try  $(/usr/bin/date +%w)
johanntagleAuthor Commented:
Ended up with this (both options work):


dow=`/bin/date +%w`
echo $dow
if [ $dow = "1" ]; then
  echo "dow works"
   echo "dow doesnt work"

if [ `/bin/date "+%w"` = "1" ]; then
  echo "`date \"+%w\"` -eq 0 works"
  echo "`date \"+%w\"` -eq 0 doesnt work"

I swear I tried those before..... maybe too much caffeine is getting to me.  Anyway thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.