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?
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.
Augeas has a Cron module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With