[PLUG] Re: cron job won't run
Keith Lofstrom
keithl at kl-ic.com
Sat Mar 5 07:08:29 UTC 2005
> Cron job that doesn't run ...
You should always run cron as if you have no environment, no path,
nothing. So all commands should be sourced from root, for example:
/usr/bin/rsync -Cavz ...
... --exclude-from=/home/mike/exclude.txt ...
/usr/bin/scp ...
/bin/echo `/bin/date` >> ...
And always replace ~ with your full address. It ain't /home/mike
if it ain't you runnin' it. Or as Dorothy said, "there's no place
like home" :-)
Yes, sometimes cron knows. But sometimes it knows the wrong thing;
it does not use your identical path or environment. Never assume
anything about a script's environment (cron or otherwise), and it
will be more robust.
If the full path is too much typing, consider shell variables.
Here are some you might define:
RSYNC='/usr/bin/rsync'
EXCLUDE='/home/mike/exclude.txt' # or wherever
RSYNCOPTS="-Cavz --delete --exclude-from=$EXCLUDE"
ARRAY='mike at opsis:Array'
TARGET='/home/backup'
... and your commands become
$RSYNC $RSYNCOPTS $ARRAY/Marketing $TARGET
$RSYNC $RSYNCOPTS $ARRAY/Accounting $TARGET
Now it becomes easy to make this into a loop, for all your sources:
for source in Marketing Accounting
do
$RSYNC $RSYNCOPTS $ARRAY/$source $TARGET
done
You can do the same with echo, scp, date, etc...
Shell variables (near the start of the program) are Your Friends.
They save you a lot of retyping if something changes, or you decide
to use a different command or re-arrange your files. If implemented
correctly, shell variables also allow you to radically change how
stuff works; for example, using a function that calls logger instead
of an scp to an over-written log file.
- - -
Lastly, if you are doing backups, may I suggest you look at dirvish?
( www.dirvish.org ) It is a bit of overkill for what you are doing
now, but when you develop a full backup procedure, with history and
incrementals and security and file restores and such, you will find
dirvish much easier to maintain than re-inventing the wheel. And
the upgrades are free!
Happy hacking!
Keith
--
Keith Lofstrom keithl at keithl.com Voice (503)-520-1993
KLIC --- Keith Lofstrom Integrated Circuits --- "Your Ideas in Silicon"
Design Contracting in Bipolar and CMOS - Analog, Digital, and Scan ICs
More information about the PLUG
mailing list