Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 21
I have a script that will run perfectly when executed at the command line, but when executing it through cron it fails in this function. Any thoughts as to why ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55

    Script runs manually, but fails in cron


    I have a script that will run perfectly when executed at the command line, but when executing it through cron it fails in this function. Any thoughts as to why would be appreciated:

    HTML Code:
    function checkup (){
      D1=`cut -d, -f1,2 dontDelete`
      D2=`echo $D1 | sed -e 's/://g' -e's/-//g' -e 's/ //g'`
        if [ "$FINAL_DATE" = "$D2" ]; then
          echo "Nothing to do." > /dev/null 2>&1
        elif [ "$FINAL_DATE" -gt "$D2" ]; then
          echo $DATE > dontDelete
          mail -s "$SUB $SUBJECT" $EMAIL < content.txt
        elif [ "$FINAL_DATE" -lt "$D2" ]; then
          echo "This worked" > /dev/null 2>&1
        else
          echo "An unknown error has occurred in the checkup function." > error
          mail -s "emailNews Script Error" $ADMIN < error
        fi
    }
    I keep failing and going to my ELSE statement: AN UNKNOWN ERROR HAS OCCURRED IN THE CHECKUP FUNCTION

    Thanks in advance.

    UPDATE: I did narrow it down a bit. It appears that my vars D1 and D2 are not being set. They work fine when run at the command line (execution of the script I mean), but not via cron. I bet if I can resolve that issue that it will fix the main issue. If I set D1 and D2 to zero at the beginning of the script before the function is called, they both come out empty. It appears strictly to be something with these 2 lines:

    HTML Code:
      D1=`cut -d, -f1,2 dontDelete`
      D2=`echo $D1 | sed -e 's/://g' -e's/-//g' -e 's/ //g'`

  2. #2
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55
    For those interested, here is all of the code in case it helps. Please note that I don't program and therefore it may be hard to follow, there may be simpler ways of doing things, and so forth. I just did it best as I know how:

    HTML Code:
    #=========== Declare Variables ===========#
    HOST=localhost                     # Location of mysql database
    DB=dbname                          # Name of database
    DB_USER=user                       # Database username
    DB_PASS=password                # Databese password
    SUB="NEWS: "                       # Start subject line with this
    EMAIL=me@mydomain.com      # Email to which updates will be sent
    ADMIN=admin@mydomain.com  # Admin email to which errors will be sent
    TITLE=cTitle
    BODY=cBody
    TABLE=news_entries
    ID=nEntryId
    DADD=dAdded
    #=============== Functions ===============#
    function findnum (){
      wc -l find.txt > wc.txt
      parse
    }
    
    function parse(){
      sed -e 's/ /,/g' wc.txt > wc2.txt
      rm -rf wc.txt
      cut -d, -f1 wc2.txt > wc.txt
      NUM=`cat wc.txt`
    }
    
    function cleanup (){
      rm -rf find.txt wc.txt wc2.txt subject.txt content.txt temp.txt date.txt date2.txt error
    }
    
    function getdate (){
      echo `mysql -s --user=$DB_USER --password=$DB_PASS --database=$DB -e "SELECT $DADD FROM $TABLE" | sort -r` > date.txt
      sed -e 's/ /,/g' date.txt > date2.txt
      D1=`cut -d, -f1,2 date2.txt > date.txt`
      rm -rf date2.txt
      DATE=`sed -e 's/,/ /g' date.txt`
    }
    
    function transform (){
      FINAL_DATE=`echo $DATE | sed -e 's/://g' -e 's/-//g' -e 's/ //g'`
    }
    
    function checkup (){
      D2=`cut -d, -f1,2 dontDelete`
      D2=`echo $D1 | sed -e 's/://g' -e's/-//g' -e 's/ //g'`
        if [ "$FINAL_DATE" = "$D2" ]; then
          echo "Nothing to do." > /dev/null 2>&1
        elif [ "$FINAL_DATE" -gt "$D2" ]; then
          echo $DATE > dontDelete
          mail -s "$SUB $SUBJECT" $EMAIL < content.txt
        elif [ "$FINAL_DATE" -lt "$D2" ]; then
          echo "This worked" > /dev/null 2>&1
        else
          echo "An unknown error has occured in the checkup function." > error
          echo "DATE:" $DATE >> error
          echo "FINAL_DATE:" $FINAL_DATE >> error
          echo "D1:" $D1 >> error
          echo "D2:" $D2 >> error
          mail -s "emailNews Script Error" $ADMIN < error
        fi
    }
    
    #================= Code ==================#
    getdate
    
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $TITLE FROM $TABLE" > find.txt` > /dev/null 2>&1
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $TITLE FROM $TABLE WHERE $DADD='$DATE'" > subject.txt` > /dev/null 2>&1
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $BODY FROM $TABLE WHERE dAdded='$DATE'" >> content.txt` > /dev/null 2>&1
    
    SUBJECT=`cat subject.txt`
    CONTENT=`cat content.txt`
    
    transform
    checkup
    cleanup

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Alrighty, well, first off, your echo statements confuse me:
    Code:
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $TITLE FROM $TABLE" > find.txt` > /dev/null 2>&1
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $TITLE FROM $TABLE WHERE $DADD='$DATE'" > subject.txt` > /dev/null 2>&1
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $BODY FROM $TABLE WHERE dAdded='$DATE'" >> content.txt` > /dev/null 2>&1
    You're just running these commands, why not just run them?
    Code:
    mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $TITLE FROM $TABLE" > find.txt 2>/dev/null
    mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $TITLE FROM $TABLE WHERE $DADD='$DATE'" > subject.txt 2>/dev/null
    mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $BODY FROM $TABLE WHERE dAdded='$DATE'" >> content.txt 2>/dev/null

    Anyway, I believe the problem lies in this statement here:
    Code:
    D2=`cut -d, -f1,2 dontDelete`
    See, you're running cut on the file "dontDelete", but what is this file? When you run the script via commandline, you know your working directory that the file is relative to, but you do NOT know your CWD when it runs through cron.

    Try providing the full path to the file, and see if that works.

  4. #4
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55
    Thanks for the info. I cleaned up the mysql queries and added the following:

    HTML Code:
    CWD=/my/current/path/to/script/
    FILE=dontDelete
    
    function checkup (){
      D2=`cut -d, -f1,2 $CWD$FILE`
      D2=`echo $D1 | sed -e 's/://g' -e's/-//g' -e 's/ //g'`
        if [ "$FINAL_DATE" = "$D2" ]; then
          echo "Nothing to do." > /dev/null 2>&1
        elif [ "$FINAL_DATE" -gt "$D2" ]; then
          echo $DATE > dontDelete
          mail -s "$SUB $SUBJECT" $EMAIL < content.txt
        elif [ "$FINAL_DATE" -lt "$D2" ]; then
          echo "This worked" > /dev/null 2>&1
        else
          echo "An unknown error has occured in the checkup function." > error
          mail -s "emailNews Script Error" $ADMIN < error
        fi
    }
    I echoed out $CWD$FILE to make sure it looked correct, and it did, but I ended up with the same result. Any more ideas for me to try?

    In addition, all of the other files I use during this process, such as find.txt, content.txt, date.txt and so on are being created and modfied correctly in the CWD despite not being defined with a full path.

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,258
    Hi.
    Code:
    function checkup (){
      D2=`cut -d, -f1,2 $CWD$FILE`
      D2=`echo $D1 | sed -e 's/://g' -e's/-//g' -e 's/ //g'`
    ...
    Why is D2 being set twice? ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401
    You should determine what command fails. If you're running script with bash (maybe it works with any shell..), a solution to debug is testing the value of the implicit parameter $?.

    If the value is 0, it means that the last command was successfull, otherwise the value is set to any number different from 0.

    Apart that, you should print what values have $D2 and $FINAL_DATE... if your script prints "An unknown error has occured in the checkup function", it mean there is some bug ($FINAL_DATE isn't less, equals, or greater than $D2).

    I don't use sed, but maybe the bug comes from there...
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  7. #7
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    510
    You can gather more helpful information by adding it to your email message:
    Code:
        else
          echo "An unknown error has occured in the checkup function." > error
          echo "DATE = '$DATE'" >> error
          echo "D1 = '$D1'" >> error
          echo "D2 = '$D2'" >> error
          echo "FINAL_DATE = '$FINAL_DATE'" >> error
          echo "$CWD$FILE contains '$(cat $CWD$FILE)'" >> error
          echo "PWD is $(pwd)" >> error
          mail -s "emailNews Script Error" $ADMIN < error
        fi

  8. #8
    Just Joined! phoch00's Avatar
    Join Date
    Sep 2006
    Location
    Dallas, TX
    Posts
    32
    Is it possible that you have an environmental variable that is set when running it interactively that isn't present for the cron user? Also, I don't see you explicitly calling any shell i.e.

    #!/bin/sh

    Could it be that your shell that you are running interactively is different than what is called via cron causing the problem?

  9. #9
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55
    OK, I redid the whole script, and experience the same problem. I am really stuck as to why this will not work via cron:

    HTML Code:
    #!/bin/bash
    
    ##############################################################
    #  This script is desgined to detect when new new posts have #
    #  been written and email subscribed users with the updated  #
    #  news.                                                     #
    #                                                            #
    #  Written by:  jdh239 9-25-2006                        #
    ##############################################################
    
    #=========== Declare Variables ===========#
    HOST=localhost                          # Location of mysql database
    DB=mydatabase                         # Name of database
    DB_USER=myuser                      # Database username
    DB_PASS=mypassword              # Databese password
    DB_TABLE=news_entries            # Do not edit
    DB_TITLE=cTitle                          # Do not edit
    DB_BODY=cBody                       # Do not edit
    DB_ID=nEntryId                            # Do not edit
    DB_DADD=dAdded                     # Do not edit
    
    DOMAIN="mydomain.com"
    CWD="/var/www/html/mydomain.com/news/script/" # Script location
    SUB="NEWS: "                            # Start subject line with this
    EMAIL=me@mydomain.com       # Email to which updates will be sent
    ADMIN=admin@mydomain.com  # Admin email to which errors will be sent
    
    DATE_FILE=date
    TITLE_FILE=title
    BODY_FILE=body
    DONT_DELETE=dontDelete
    #=============== Functions ===============#
    function MAKEFILES (){
     ls -l > $CWD$BODY_FILE
    }
    
    function SORTDATES (){
      DATE=`head -1 date`
      NEWDATE=`head -1 date | sed -e 's/://g' -e 's/ //g' -e 's/-//g'`
      COMPAREDATE=`head -1 $CWD$DONT_DELETE`
    }
    
    function TESTER (){
      if [ $NEWDATE == $COMPAREDATE ]; then
        echo "Nothing to do." > /dev/null 2>&1
      elif [ $NEWDATE -gt $COMPAREDATE ]; then
        echo "Email Users." > /dev/null 2>&1
        echo $NEWDATE > $CWD$DONT_DELETE
        mail -s "$SUB $SUBJECT" $EMAIL < $CWD$BODY_FILE
      elif [ $NEWDATE -lt $COMPAREDATE ]; then
        echo "Older Entry.  Doing nothing." > /dev/null 2>&1
      else
        echo "An Unknown Error Has Occurred in the TESTER function for the domain: $DOMAIN." > $CWD$BODY_FILE
        mail -s "News Script error for $DOMAIN" $ADMIN < $CWD$BODY_FILE
      fi
    }
    
    function CLEANUP (){
      rm -rf $CWD$DATE_FILE $CWD$TITLE_FILE $CWD$BODY_FILE
    }
    
    function SETUPEMAIL (){
      SUBJECT=`cat $CWD$TITLE_FILE`
      BODY=`cat $CWD$BODY_FILE`
    }
    #================= Code ==================#
    MAKEFILES
    
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $DB_DADD FROM $DB_TABLE ORDER BY '$DB_DADD' DESC" > $CWD$DATE_FILE` > /dev/null 2>&1
    
    SORTDATES
    
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $DB_TITLE FROM $DB_TABLE WHERE $DB_DADD='$DATE'" > $CWD$TITLE_FILE` > /dev/null 2>&1
    echo `mysql -s -r --user=$DB_USER --password=$DB_PASS --database=$DB --execute="SELECT $DB_BODY FROM $DB_TABLE WHERE $DB_DADD='$DATE'" > $CWD$BODY_FILE` > /dev/null 2>&1
    
    SETUPEMAIL
    TESTER
    CLEANUP

  10. #10
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Alright. So you still get that error e-mail sent to you?

    Add these lines below the mail call in the else statement:
    Code:
    echo "NEWDATE: $NEWDATE" > /tmp/dumb_script
    echo "COMPAREDATE: $COMPAREDATE" >> /tmp/dumb_script
    Check what the output is there. See what the values are.

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •