cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with scripting.

SOLVED
Go to solution
mjos
Super Advisor

Need help with scripting.

Hi,
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.

#!/usr/bin/sh
set -u
SEARCH=/tmp/xyz
GZDIR=/tmp/xyz/archive
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} #...to remove what you archived...
exit 0
3 REPLIES
Pete Randall
Outstanding Contributor
Solution

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.)


Pete

Pete
mjos
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.
Dennis Handly
Acclaimed Contributor

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.