tags:

views:

356

answers:

4

Is there such a thing as a Cron API?

I mean, is there a programmatic way of adding/removing Cron jobs without stepping onto Cron's toes?

A: 

Not as such, though scripting a modification using crontab isn't that hard. That sort of thing (working through text tools instead of expecting a code-level API to system facilities) is part of the Unix paradigm, really.

Alternatively, a pretty trivial "cron API" can be implemented using a quick script in whatever language you prefer and "sleep". :)

Andy Ross
+4  A: 

UNIX cron's API is the filesystem. There is a crontab command for installing/editing user crontabs. The main reason for the crontab command is to enforce security restrictions on users (e.g., /etc/cron.allow and /etc/cron.deny).

System cron tabs are just files placed in /etc/cron.d (and cron.daily/weekly/monthly). No special care is needed; just drop the file in place. To quote the top of /etc/crontab:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

The format is the same as user crontabs, documented in crontab(5), but with a user field right before the command. Where SPACE means whitespace (one or more) and both 0 and 7 mean Sunday:

minute SPACE hour SPACE day-of-month SPACES month SPACE day-of-week SPACE user SPACE command

Using normal POSIX file access won't step on cron's toes. Remember, rename will always have the target name pointing to either the old or new file, never to nothing. So you can write the file to a new name and then rename it over top your old one.

Many programming languages have APIs to help with writing crontabs. For example, CPAN (Perl) has several.

derobert
So if I drop a file in /etc/cron.d/, it will get picked up by cron on its next iteration no harm done? I don't need to go through crontab?
jldupont
@Jean-Lou Dupont: Correct. You can just drop the file in /etc/cron.d.
derobert
A: 

You can definitely wrap around it. See the Whenever ruby gem.

hgimenez
+2  A: 

Augeas has a Cron module.

Josh Kelley
Interesting... thanks for the link!
jldupont