Dec 03, 2020 • 3 min read

11 comments

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.

Access email sent!

Sign Up For
More Awesome Content!

Subscribe to receive our monthly newsletters with the latest helpful content and offers from SiteGround.

Thanks!

Please check your email to confirm your subscription.

author avatar

Cal Evans

PHP Evangelist

One of the most admired people in the PHP community, who has dedicated more than 16 years to building the amazing PHP community and mentoring the next generation of developers. We are extremely honored that he is a very special friend of SiteGround too.

Comments ( 11 )

author avatar

Matt

Dec 03, 2020

Are there some real-life scenarios you could provide where this would be useful?

Reply
author avatar

Hristo Pandjarov Siteground Team

Dec 07, 2020

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.

Reply
author avatar

Wayne Stewart

Dec 18, 2020

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.

Reply
author avatar

Francis Ak

Dec 05, 2020

Great 👍

Reply
author avatar

David Pascoe

Jan 22, 2021

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.

Reply
author avatar

Hristo Pandjarov Siteground Team

Jan 25, 2021

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 :)

Reply
author avatar

Jerome Gaynor

Aug 03, 2022

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!

Reply
author avatar

Gergana Zhecheva Siteground Team

Aug 04, 2022

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.

Reply
author avatar

Ronald

Mar 05, 2024

If your client is at NYC, it's -5. So, you can set on 14:00:00 which is 9:00 AM in the morning of NYC.

Reply
author avatar

JT

Apr 17, 2024

The default time zone is UCT - is this changeable? If not is there a recommendation how to handle DST? (I am thinking GMT / BST specifically) without having to manually adjust the Cron time twice a year?

Reply
author avatar

Lina Asenova Siteground Team

Apr 19, 2024

Hey there, thank you for your question. The default timezone for our servers is set to UTC to maintain consistency across our global platform, and unfortunately, it is not changeable. For handling GMT/BST and similar cases, you would need to manually adjust the CRON job. Alternatively, you can modify the script to check for timezone changes and accommodate accordingly. If you need further assistance or have any other questions, you can reach out directly to our support team. Here are the steps: stgrnd.co/contactus/.

Reply

Start discussion

Related Posts