问题
I have a svn backup script in a redhat linux. let's it called svnbackup.sh
It works fine, when I run it in terminal.
But when I put it into crontab, it will not bring the svnserve back, even the data is backuped correctly.
What's wrong with me???
killall svnserve
tar -zcf /svndir /backup/
svnserve -d -r /svndir
回答1:
Usually, 'environment' is the problem in a cron job that works when run 'at the terminal' but not when it is run by cron. Most probably, your PATH is not set to include the directory where you keep svnserve
.
Either use an absolute pathname for svnserve or set PATH appropriately in the script.
You can debug, in part, by adding a line such as:
env > /tmp/cron.job.env
to your script to see exactly how little environment is set when your cron job is run.
回答2:
If you are trying to backup a live version of a repository, you probably should be using svnadmin hotcopy
. That said, here are a few possibilities that come to mind as to what might be wrong:
- You've put each of those statements as separate entries in your crontab (can't tell from the Q).
- The
svnserve
command takes a password, which cron, in turn, cannot supply. - The
svnserve
command blocks or hangs indefinitely and gets killed by cron. - The command
svnserve
is not in your PATH in cron.
Assuming that svnserve
does not take a password, this might fix the problem:
#! /bin/bash
# backup_and_restart_svnserve.sh
export PATH=/bin:/sbin:/usr/bin:/usr/local/bin # set up your path here
killall svnserve && \
tar -zcf /svndir /backup/ && \
svnserve -d -r /svndir >/dev/null 2>&1 &
Now, use "backup_and_restart_svnserve.sh" as the script to execute. Since it runs in the background, it should hopefully continue running even when cron executes the next task.
来源:https://stackoverflow.com/questions/4536683/why-my-svn-backup-shell-script-works-fine-in-terminal-but-fails-in-crontab