Results 1 to 6 of 6

Thread: Bash script behaving differently in crontab vs manually run

Threaded View

  1. #1
    Join Date
    Sep 2011
    Location
    Behind you!
    Beans
    584
    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.

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
  •