How to delegate within a crontab to use another file as a crontab? aka Crontab in SVN/CVS?

梦想与她 提交于 2019-12-06 03:23:37

Put your crontab file in /etc/cron.d/ - either as a checked out file or a symlink to the checked out file.

We take a very different approach to this problem on the project I'm working on.

We have a Perl script called crontab.pl that all servers call in their crontab. Doing it this way means the crontab never has to change on successive application deployments and the crontab.pl script can happily live in source control with the rest of the application source files.

We can specify different time intervals to this script with a command line parameter. The crontab looks like this:

*/5 * * * * /usr/local/apache/crontab.pl 5    > /var/log/crontab.log 2>&1 # 5 minute interval
1   * * * * /usr/local/apache/crontab.pl 60   > /var/log/crontab.log 2>&1 # 1 hour interval
1   1 * * * /usr/local/apache/crontab.pl 3600 > /var/log/crontab.log 2>&1 # 1 day interval

Then it is just a matter of reading in that command line time interval and running the tasks needed for the given interval.

Just add one (or very few) entries to crontab, and have that entry be a revision-controlled script that calls others:

0 * * * * /path/to/app/hourly.sh 0 0 * * * /path/to/app/daily.sh 0 1 * * 5 /path/to/app/weekly.sh

hourly.sh would then call everything that needs to be done hour, daily.sh would then call everything that needs to be done daily, etc.

I have found when managing large numbers of cron jobs for a single application that it is best not to have the jobs all hanging loose in the crontab, because that encourages you to overlook dependencies between them, i.e., one task that runs at 14 minutes past the hour depends on a task that runs at 12 minutes past the hour succeeding, but every so often that task takes 2.1 minutes to complete, and the other script fails, giving you a Heisenbug.

Another approach is to write your own daemon to manage these tasks.

The way I do this is put my crontab file in source control, and then in my deploy script, I replace the crontab file with what is being deployed.

You can do that like this:

crontab filename

Or for a specific user (must have superuser access for this):

crontab -u my_web_user filename

One big caveat with this is that it assumes there's only one crontab entry per user and that it's stored in SCM. If someone else edits the crontab on the server, those edits will be lost on the next deploy. So be aware of that.

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