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
Bookmarks