bash script conditional statement behaving differently when run under cron

Posted on 2012-09-03
Last Modified: 2012-09-09
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!
Question by:johanntagle
    LVL 40

    Assisted Solution

    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
    LVL 37

    Expert Comment

    by:Gerwin Jansen
    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.
    LVL 24

    Author Comment

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

    Accepted Solution

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

    Expert Comment

    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)
    LVL 24

    Author Closing Comment

    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

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now