Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron API: is there such a thing?

Tags:

linux

cron

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?

like image 910
jldupont Avatar asked Sep 24 '09 22:09

jldupont


2 Answers

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.

like image 85
derobert Avatar answered Sep 30 '22 11:09

derobert


Augeas has a Cron module.

like image 43
Josh Kelley Avatar answered Sep 30 '22 12:09

Josh Kelley