why my svn backup shell script, works fine in terminal, but fails in crontab?

若如初见. 提交于 2020-01-06 04:25:08

问题


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:

  1. You've put each of those statements as separate entries in your crontab (can't tell from the Q).
  2. The svnserve command takes a password, which cron, in turn, cannot supply.
  3. The svnserve command blocks or hangs indefinitely and gets killed by cron.
  4. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!