Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

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?                              
0
eric8888
Asked:
eric8888
  • 3
  • 2
1 Solution
 
HamdyHassanCommented:
what is $file and $TIME ?

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.

0
 
eric8888Author Commented:
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,
0
 
dt64852Commented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HamdyHassanCommented:
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/scripts
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/scripts
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





0
 
eric8888Author Commented:
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,
0
 
HamdyHassanCommented:
Very good.

That is what I was expected and I did mentioned that at my post twice

"other tip of cron,
you need full path for any file you are dealing with."

"in order to work from cron job is to define pathname for any command or file"

because this is the major issue when moving script to run from cron job.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now