Showing results for 
Search instead for 
Did you mean: 

Need help with scripting.

Go to solution
Super Advisor

Need help with scripting.

I have the following script for purging & archive old files from tmp. When I manually run the script, it runs fine & give archive.gz file.
But when I schedule the same script to run in cron, it produces a 0 bytes file.
Can somebody please help what is going wrong when cron runs the same script.

set -u
GZFILE=${GZDIR}/arch_$(date +%Y%m%d).gz
cd ${SEARCH} || exit 1
FILES=$(find . -type f -mtime +10)
[ -z "${FILES}" ] && { echo "no files older than 10-days"; exit 1; }
tar -cvf - ${FILES} | gzip - > ${GZFILE}
find ${GZDIR} -type f -name "*.gz" -mtime +10 -exec rm {} +
#rm ${FILES} remove what you archived...
exit 0
Pete Randall
Outstanding Contributor

Re: Need help with scripting.

Whenever we come across a script that runs fine from the command line yet does not work from cron, it is nearly always because of the sparse environment provided by cron. Check the man pages and you will find that the PATH for cron is minimal, and many environment variables are not set. The first thing I would do is make sure you either put a PATH statement in the beginning of your script or use full path names for all your commands (tar=/usr/bin/tar, find=/usr/bin/find, etc.)


Super Advisor

Re: Need help with scripting.

Silly me, thanks a lot Pete.
Putting the full path names solved my problem.
Thanks - I have assigned points.

Re: Need help with scripting.

>Putting the full path names solved my problem.

It seems that gzip was the only one that needed it.

>Pete: tar=/usr/bin/tar, find=/usr/bin/find, etc.)

None of these are really needed, they are in the default cron PATH.