eric8888
asked on
Question about gzip in cron job
I created a script at sun env. to back up development files. following is part of the code
cp -p $file_record $file_record.$TIME
chmod 666 $file_record
gzip $file_record.$TIME
mv $file_record.$TIME.gz $file_record.gz
tar rvf $WORKFILE $file_record.gz
/bin/rm $file_record.gz
TIME is system time (to make sure a uniqure file name).
This script is working fine when typing the command at the prompt. After I set a cron job to run it,"gzip $file_record.$TIME" is failed. Does anyone can explain it?
cp -p $file_record $file_record.$TIME
chmod 666 $file_record
gzip $file_record.$TIME
mv $file_record.$TIME.gz $file_record.gz
tar rvf $WORKFILE $file_record.gz
/bin/rm $file_record.gz
TIME is system time (to make sure a uniqure file name).
This script is working fine when typing the command at the prompt. After I set a cron job to run it,"gzip $file_record.$TIME" is failed. Does anyone can explain it?
ASKER
That's just a variable in the script. They are used to create unique file name. You can just use any file name to replace $file.$TIME.
Thanks,
Thanks,
You don't say what error you got. If you have not already done so, direct your stdout and stderr to a file and rerun.
10 3 * * 0 yourscript > yourlogfile 2>&1
I think you will see either an error message or an unanswered warning in the log.
10 3 * * 0 yourscript > yourlogfile 2>&1
I think you will see either an error message or an unanswered warning in the log.
ok that is good, now one thing you need to add to your script in order to work from cron job is to define pathname for any command or file
so instead of
cp -p $file_record $file_record.$TIME
chmod 666 $file_record
you should us
MyHome=/home/myname/script s
cp -p $MyHome/$file_record $MyHome/$file_record.$TIME
chmod 666 $MyHome/$file_record
Try that and let me know
One advice, use "echo" command between actions to debug
MyHome=/home/myname/script s
echo $MyHome
cp -p $MyHome/$file_record $MyHome/$file_record.$TIME
echo $MyHome/$file_record.$TIME
and for sure at crontab you need to put output messages to output file as dt64852n said
Hamdy
so instead of
cp -p $file_record $file_record.$TIME
chmod 666 $file_record
you should us
MyHome=/home/myname/script
cp -p $MyHome/$file_record $MyHome/$file_record.$TIME
chmod 666 $MyHome/$file_record
Try that and let me know
One advice, use "echo" command between actions to debug
MyHome=/home/myname/script
echo $MyHome
cp -p $MyHome/$file_record $MyHome/$file_record.$TIME
echo $MyHome/$file_record.$TIME
and for sure at crontab you need to put output messages to output file as dt64852n said
Hamdy
ASKER
Finally I figured out the problem.
The path for cron job is /usr/sbin, and gzip is only in /opt/bin (I am shocked that gzip is not in .bin and /usr/bin). Since my PATH does not include /opt/bin/ (but includes /opt/home/peri/bin under which gzip is), the script was working fine from the command mode, but not working in cron job. I changed the script to run gzip as following,
"cp -p $file_record $file_record.$TIME
chmod 666 $file_record
/opt/bin/gzip $file_record.$TIME
mv $file_record.$TIME.gz $file_record.gz
tar rvf $WORKFILE $file_record.gz
/bin/rm $file_record.gz"
Note that the gzip call is using full path. The script is working fine now.
I have sent a comment to sun to suggest them add gzip in /bin, /usr/sbin and any possible pathes.
Thanks for all your suggestions and advices,
The path for cron job is /usr/sbin, and gzip is only in /opt/bin (I am shocked that gzip is not in .bin and /usr/bin). Since my PATH does not include /opt/bin/ (but includes /opt/home/peri/bin under which gzip is), the script was working fine from the command mode, but not working in cron job. I changed the script to run gzip as following,
"cp -p $file_record $file_record.$TIME
chmod 666 $file_record
/opt/bin/gzip $file_record.$TIME
mv $file_record.$TIME.gz $file_record.gz
tar rvf $WORKFILE $file_record.gz
/bin/rm $file_record.gz"
Note that the gzip call is using full path. The script is working fine now.
I have sent a comment to sun to suggest them add gzip in /bin, /usr/sbin and any possible pathes.
Thanks for all your suggestions and advices,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to export these variables in the main script.
then cron job will call that main script
other tip of cron,
you need full path for any file you are dealing with.