What is CRON and What You Can Use It For?
“What the heck is a cron?” I field this question at least once a month from non-developers. It’s a great question. I’m going to break it into two questions though.
“What is a CRON?”
“What is WP-CRON?”
What is a CRON?
At its heart, a cron is a “time-based scheduler”. It handles tasks that need to be done on a regular basis and at a specific time. As an example, if you want your WordPress blog to display the weather forecast in the header, then each morning you need to go get the weather forecast. Yes, you could hire someone to log in each morning, go get the forecast and paste it into a widget.
A better plan is to have a program that runs each morning and talks to an API to fetch the day’s forecast and update your database for you. The program that runs your weather fetching program is called a CRON. The name is derived from “chronological” which roughly translates into “in order of time”.
Most systems these days have some concept of a cron. Unix based systems (Unix, Linux, macOS, etc.) actually have a version of a traditional cron. While some might put a nice graphical interface on them, they all boil down to a program named cron and a file named crontab.
The program cron is always running in the background and every minute it looks at the crontab and figures out if something needs to be done. If not, it goes back to sleep.
The crontab file contains when a program should be run and which program should be run. Each line represents a different task. They look something like this.
1 0 * * * ~/fetchForcast.sh
While this may look cryptic, all it is telling cron is that at 12:01 AM every day, run a program called fetchForcast.sh. Note that time added in the Cron tool is in UTC by default. Here is an easy guide to reading a crontab.
# ┌───────────── minute (0 – 59)
# │ ┌───────────── hour (0 – 23)
# │ │ ┌───────────── day of the month (1 – 31)
# │ │ │ ┌───────────── month (1 – 12)
# │ │ │ │ ┌───────────── day of the week (0 – 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
1 0 * * * ~/fetchForcast.sh
Now that you have the key, it’s pretty easy, huh?
That really is all there is to a traditional cron. Most hosts like SiteGround allow you access to the cron for your system. Sometimes you have to edit the crontab manually, but many hosts have a much better interface for you to use. Either way, you have the ability to run programs at a specific time and on a regular basis.
What is WP-CRON
Like most things, WordPress does things just a little differently. Because many plugin authors needed to be able to schedule things to happen regularly, and because many WordPress site owners don’t know where their crontab is, much less how to edit it, WordPress re-invented the cron.
At its core, WP-CRON acts like a traditional cron in that a developer can “schedule” a task to be done on a regular basis. However, unlike a traditional cron, WordPress does not have a program that is always running in the background of your server. So to make this world, WP-CRON is a process that is called every time a page is viewed.
Comments ( 8 )
Thanks! Your comment will be held for moderation and will be shortly published, if it is related to this blog article. Comments for support inquiries or issues will not be published, if you have such please report it through
Matt
Are there some real-life scenarios you could provide where this would be useful?
Hristo Pandjarov Siteground Team
There are endless use cases for cron jobs - autosaves, email sending, publishing content on a future date. Basically everything that relies on an action to happen in a certain date or period of time.
Wayne Stewart
Here is a real life scenario what my company uses cron jobs to do. When a client adds a new picture to there website we have a cron job that run a compression plugin that makes pictures smaller and easier for a web browsers to load. That helps improve the performance of a application.
Francis Ak
Great 👍
David Pascoe
There are quite stringent restrictions on cron jobs on Siteground servers though, right? So cron is not super useful at Siteground, only for simple, maximum hourly small tasks? Anything more often or needing a little more CPU will get terminated by the resource limits.
Hristo Pandjarov Siteground Team
There is no difference in the limits if a PHP script is executed via your browser or a cron job. There is only a limit not to set up crons too close to each other because they can pile up and result in peaks if multiple run at the same time but that's to be expected really. So, you can use them for whatever you would otherwise run a sript for, they are not limited to small tasks :)
Jerome Gaynor
Can you make a note somewhere (preferably in the actual tool), but at least here - that specifies the TIME ZONE that the Cron tool uses? Every couple years I have to adjust a Cron job and every time I get confused by the fact that you can't change the time zone, and I can never remember what the time zone actually is. It's GMT, correct? Typically, my clients want automatic jobs performed at 9 am THEIR time, not GMT! Thanks!
Gergana Zhecheva Siteground Team
This is a good suggestion, Jerome! The default time zone for this feature is UCT. We have added a clarification in the article, thank you for your suggestion.
Start discussion
Thanks! Your comment will be held for moderation and will be shortly published, if it is related to this blog article. Comments for support inquiries or issues will not be published, if you have such please report it through