Results 1 to 6 of 6

Thread: Bash script behaving differently in crontab vs manually run

Hybrid View

  1. #1
    Join Date
    Sep 2011
    Location
    Behind you!
    Beans
    515
    Distro
    Ubuntu 12.04 Precise Pangolin

    Question Bash script behaving differently in crontab vs manually run

    Greetings.

    I have a script that purges files from a folder and I would like a record (log file) of what files were purged.

    I wrote the following snippet and it works when executing it from the command line:

    Code:
    LOGFILE="/path/to/logfiles/purge-folder.log"
    echo "`date +%Y-%m-%d_%H:%M:%S` - Script started." >> ${LOGFILE}
    for file in "/path/to/folder/*.txt"
    do
      echo " - INFO: Deleting ${file}" >> ${LOGFILE}
      rm ${file}
    done
    echo "`date +%Y-%m-%d_%H:%M:%S` - Script completed." >> ${LOGFILE}
    When I run it from the command line, the log file looks like this (shows each file deleted individually):

    Code:
    2013-03-01_10:43:35 - Script started.
     - INFO: Deleting /path/to/folder/20130227.txt
     - INFO: Deleting /path/to/folder/20130228.txt
     - INFO: Deleting /path/to/folder/20130301.txt
    2013-03-01_10:43:35 - Script completed.
    When run from crontab, the log file looks like this (no matter how many files are deleted):

    Code:
    2013-03-02_10:00:01 - Script started.
     - INFO: Deleting /path/to/folder/*.txt
    2013-03-02_10:00:01 - Script completed.
    This is how my root crontab looks:

    Code:
    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    0 10 * * * /path/to/scripts/purge-folder.sh > /dev/null 2>&1
    Any idea on how to get the script to behave in crontab like it does at the command-line?

    Thanks,
    LHammonds
    Last edited by LHammonds; March 8th, 2013 at 04:10 PM.

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Bash script behaving differently in crontab vs manually run

    Remove excessive quoting
    Code:
    LOGFILE="/path/to/logfiles/purge-folder.log"
    echo "`date +%Y-%m-%d_%H:%M:%S` - Script started." >> ${LOGFILE}
    for file in /path/to/folder/*.txt # excessive double quotes removed from this line
    do
      echo " - INFO: Deleting ${file}" >> ${LOGFILE}
      rm ${file}
    done
    echo "`date +%Y-%m-%d_%H:%M:%S` - Script completed." >> ${LOGFILE}

  3. #3
    Join Date
    Sep 2011
    Location
    Behind you!
    Beans
    515
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash script behaving differently in crontab vs manually run

    I'll give that a shot but I added the quotes to see if that would help solve the problem.

    EDIT: Ha! That did it! Not sure what I had done before since I added the quotes to see if that would resolve the problem but it is working now. Thanks
    Last edited by LHammonds; March 8th, 2013 at 04:08 PM.

  4. #4
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Bash script behaving differently in crontab vs manually run

    Shell globs (*) don't get expanded inside double quotes. Besides, you will still get the message
    Code:
    - INFO: Deleting /path/to/folder/*.txt
    if there are no files in the folder matching this pattern. You may choose to ignore it, or test for presense of any files to be deleted beforehand. If your script is bash (as opposite to /bin/sh), you also can set shopt -s nullglob before entering the loop.
    Last edited by schragge; March 8th, 2013 at 04:13 PM.

  5. #5
    Join Date
    Sep 2011
    Location
    Behind you!
    Beans
    515
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bash script behaving differently in crontab vs manually run

    Good point about the file check. I should do that no matter what before an rm command.

    Script modified as such:

    Code:
    LOGFILE="/path/to/logfiles/purge-folder.log"
    echo "`date +%Y-%m-%d_%H:%M:%S` - Script started." >> ${LOGFILE}
    for file in /path/to/folder/*.txt
    do
      if [ -f ${file} ]; then
        echo " - INFO: Deleting ${file}" >> ${LOGFILE}
        rm ${file}
      fi
    done
    echo "`date +%Y-%m-%d_%H:%M:%S` - Script completed." >> ${LOGFILE}

  6. #6
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Bash script behaving differently in crontab vs manually run

    If you don't have thousands of .txt files in the folder then I'd rewrite it like this (sed is optional, in case you don't like the output of rm -v)
    Code:
    LOGFILE="/path/to/logfiles/purge-folder.log"
    echo "`date +%Y-%m-%d_%T` - Script started." >> ${LOGFILE}
    rm -fv /path/to/folder/*.txt | sed 's/^removed/ - INFO: Deleting/'  >> ${LOGFILE}
    echo "`date +%Y-%m-%d_%T` - Script completed." >> ${LOGFILE}

Tags for this Thread

Bookmarks

Posting Permissions

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