• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 825
  • Last Modified:

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!
2 Solutions
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now