message

What is CRON and What You Can Use It For?

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.

On busy sites, this works fine. However, if your site isn’t busy, a task scheduled for 2:00 AM might be run at 5:24 AM if nobody visits your site until then. Sometimes this is ok, other times this is a problem.

If the tasks you need to run are time-sensitive and have to be run at the time scheduled, WP-CRON is not the scheduler you want to use. If on the other hand, the tasks you need to be done can happen “around” the time you schedule them, then WP-CRON is fine. Again, a lot depends on how busy your site is.

What are the alternatives?

If you have tasks that are time-sensitive and your host does not allow you access to the system’s cron, you have 2 alternatives. First, you can switch to a host like SiteGround that gives you this access. If that’s not possible, then there are several services free or paid that are nothing more than cron services.

They run cron and you can set a job to run via a nice web interface. The job would use a program like curl or wget (think of them as headless browsers) that call URLs on your site to fire a specific task. Most plugins that require a cron will give you the URL to call if you want to use an external cron. All you have to do is paste the URL in, set the time for it to run and you are done.

CRON is a valuable tool and once you understand how to work with it, you will find more uses for it. If you have plugins, then I can almost guarantee that your site has wp-cron jobs running. If you are curious, go to the WordPress plugin repository and search for cron. There are plugins you can install that will show you all the WP-CRON activity on your site. Be very careful though. Plugins set these for a reason. If you decide you don’t like one and delete it, the plugin that depends on the job will stop working.

Related Articles

Back to top button