tags:

views:

140

answers:

3

Currently, I am editing my production server's crons by typing crontab -e. I would like to store my crontab definitions inside my project, and have my system to load them from there.

Is there any way I can set up the servers existing crontabs to point to an include like this?

IncludeOtherCrontab /path/to/my/project/project.crontab

My system is CentOS release 5.4 (Final)

+2  A: 

I don't know whether you can include other files in your base crontab file, but maybe you could just order in your crontab to reload/alter it's contents?

Say, every 5 minutes cron executes your bash script which reads your 'project.crontab' and appends it to your crontab file.

pajton
This is a good idea. The most elegant solution would be one like Byron Whitlock's, but if none of those work then this is a terrific backup idea. It lets me store the cron specification in my project where I want it.
Gattster
Actually, instead of the cron updating every 5 minutes, it could just be part of my deploy script to update the cron.
Gattster
Yes, that would work too. Actually it depends on when does your 'project.crontab' change and when do you want to reload it. If it only changes when you rebuild/deploy your project then it is probably the best to update crontab immediately.
pajton
See below on how to do that without kicking `crond` in the groin every 5 minutes (assuming your deployment script can issue commands on the target node, not just upload files.)
vladr
A: 

The easiest way to do this is to put your crontab entry into a file under /etc/cron.d/. Take a look at some existing files there to see the syntax. You can either copy your file there manually (or via an installer such as rpm) or just use a symlink to the file located in your installed directory.

ar
+2  A: 

To do this portably (i.e. not using /etc/cron.d), assuming your deployment script must coexist with third-party crontab entries and provided your deployment script can execute commands on the target node, not just upload files:

test -f project.crontab && { { crontab -u user -l | awk '/^#BEGIN PROJECT SECTION/{hide=1;next}/^#END PROJECT SECTION/{hide=0;next}!hide{print}' ; echo '#BEGIN PROJECT SECTION' ; cat project.crontab ; echo '#END PROJECT SECTION' ; } | crontab -u user ; }

The above verifies that your project-specific crontab section exists, filters it from the given user's crontab (if it already is there, does nothing otherwise), appends the project section, then reloads the lot.

vladr
This looks great. TY
Gattster