2

我有以下命令来显示对 subversion 存储库的更改

svn log -v -r{$(date +%Y-%m-%d)}:HEAD http://therepository | awk '/^r[0-9]+ / {user=$3} {if (user=="username") {print $1 $2 $3}}' | mail -ne -s'Users SVN commits' email@email.com

它在命令行中运行良好。

当我将其粘贴到 crontab 中时,我收到以下错误消息:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
/bin/sh: -c: line 1: syntax error: unexpected end of file

这需要如何引用才能起作用?

4

2 回答 2

6

When using cron, avoid the hassle of such issues by putting everything into a shell script, then call the shell script from cron.

This approach is commonly used and a good idea because:

  • It makes your command easily testable (you don't have to do stupid things like schedule an execution in one minute's time)
  • Easy to manually invoke if you have to, eg in case the job failed, ops can re-run without touching crontab - also jobs can be invoked from other jobs, eg a final job that checks if all jobs ran OK and re-runs those that didn't
  • It separates what is executed from when it's executed - leaving cron to do (only) what it does best: scheduling
  • It gives you full access to shell script features, or using different shells like perl
  • It keeps crontab clean and easy to read
  • Anyone developing/maintaining cron tasks knows where to look if you use a consistent directory for cron tasks (eg /opt/cron or whatever)
  • You can put your cron tasks under source control - too often shell scripts are overlooked for source control, but they are code and therefore benefit from source control

For example:

dosomething.sh:

svn log -v -r{$(date +%Y-%m-%d)}:HEAD http://therepository | awk '/^r[0-9]+ / {user=$3} {if (user=="username") {print $1 $2 $3}}' | mail -ne -s'Users SVN commits' email@email.com

plus

cron 0 22 * * * /opt/cron/dosomething.sh
于 2011-08-14T21:05:28.703 回答
1

最简单的做法是将其保存为 bash 脚本,然后从 cron 运行该脚本。

(我会将此作为评论而不是答案,但不允许我发表评论)

于 2011-08-14T21:07:30.857 回答