Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How can I programmatically create a new cron job?

I want to be able to programatically add a new cron job, what is the best way to do this?

From my research, it seems I could dump the current crontab and then append a new one, piping that back into crontab:

(crontab -l ; echo "0 * * * * wget -O - -q http://www.example.com/cron.php") | crontab -

Is there a better way?

Answer*

Cancel
9
  • 6
    Just make sure the version of cron in use supports /etc/cron.d/. Most modern Linux distributions do. Commented Mar 6, 2009 at 11:09
  • 14
    One potential caveat to this is the frequency at which cron picks up new additions to this folder (once per hour). If you're expecting your job to begin running right away, beware. Commented Apr 17, 2014 at 2:53
  • 9
    @JonathanK, do you happen to know if cron can be asked to re-scan /etc/cron.d/*? It's hard to know if something is working if one has to wait for an hour to check! Commented Dec 9, 2014 at 15:57
  • 1
    At least on CentOS & RHEL, "The cron daemon checks the /etc/crontab file, the /etc/cron.d/ directory, and the /var/spool/cron/ directory every minute for any changes." centos.org/docs/5/html/5.2/Deployment_Guide/… access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/… Commented Feb 10, 2017 at 0:33
  • 4
    how should perms set for this file? chmod +x /etc/cron.d/per_minute ? Commented Sep 3, 2017 at 9:03