If you’re having trouble remembering to run a script on a regular basis, or you just want to invest your time and energy in something more creative, it’s high time you put cron to good use.
Before you dive in though, you probably want to know what cron is, what exactly a cron job is, and what it can do for you.
Cron is a time-based task scheduler in Unix-like operating systems (OS) and one of the most useful utilities you will find there. Cron was developed in 1975 at AT&T Bell Laboratories and named after Chronos, a Greek personification of time. Not only has Cron come a long way from its original inception, but it has managed to remain relevant to this day.
It’s often used to automate tasks that run in the background at specific times, e.g. B. schedule backups, monitor disk space, manage system maintenance tasks, free up space from useless files, send bulk emails and much more. All these different scheduled tasks (or jobs) are called cron jobs.
To get you started with cron jobs, learn how to set them up using cPanel, Plesk, and WordPress. But before that, let’s learn a little more about how a cron job works.
What is a cron job used for?
Cron is essentially a daemon (a computer program that runs as a background process for various purposes) that detects and runs cron jobs at specific dates and times, whether it’s a one-off or recurring task. It runs every minute, checking a number of predefined directories on your file system to see if there are any cron jobs to run.
To run a cron job, there must be three components, including a specific command or script to run, a command that runs the script at a specific time (usually set in your control panel), and an action or output from the script , which would depend on what the script in question is doing.
Cron jobs can be scheduled to run multiple times a day or at specific hours and minutes on specific days and months. To do this, you use crontabs, a list of commands that the operating system uses to schedule all kinds of tasks to be run by cron expressions at the preset time.
We should note that most scripts that use a cron job come with specific instructions so you know what needs to be set up and how.
How to set up a cron job with cPanel
The most popular Linux-based graphical user interface (GUI), cPanel simplifies the tedious tasks of managing and monitoring every aspect of your website or websites. So if you have the pleasure of using this user-friendly panel, you can use it to set up a cron job.
First, let’s start by logging into your cPanel. On the cPanel dashboard, scroll down to the “Advanced” section, look for “Cron Jobs” and click on the icon.
This will take you to the cron job interface where you can enter your email address so the cron utility can email you every time one of the cron jobs produces an output – it’s an easy way , to keep track of each job and check for possible mistakes.
After that, you should scroll down to the General Settings section and configure specifications about the cron jobs you want to add. Here you can select the schedule on which a cron job should run – once per minute, once every five minutes, once per hour, and so on.
Once the schedule is selected, the text box below will be filled in automatically, but you can customize it to suit your needs.
Lastly, type the command you want the system to run in the Command section, tap Add New Cron Job, and congratulations, you’ve set up a cron job.
How to set up a cron job with Plesk
Plesk is a web hosting platform with a control panel that allows the admin to set up websites, email accounts, DNS, and databases from a web browser.
While Plesk and cPanel serve the same purpose, they differ in terms of GUI and the servers they support. cPanel’s intuitive interface is easier to use, but its web-based control panel is only compatible with Linux servers. On the other hand, Plesk supports both Windows and Linux servers.
Similar to cPanel, setting up a cron job requires you to login with Plesk and click the Scheduled Tasks button on the right pane of the dashboard.
There you’ll see an option to add, remove, and update tasks, but also a “Settings” section, which you’ll want to visit first and set a correct time zone. After that, click on “Add task” and add all the details about the cron job you want to run including time intervals, notifications, a description and enter the actual command. That’s it, the cron job is set up.
The only small difference between setting cron jobs with cPanel and Plesk is that in the latter, the icon doesn’t say “cron jobs”.
How to set up a WordPress cron job
If you want to set up a cronjob for your WordPress website, you must first install and activate one of the cronjob plugins (like WP Crontrol, Easycron, and Advanced Cron Manager) that you can find in the WordPress .org plugin library. This is part of WordPress’ own cron system, which makes it possible to set and run all sorts of scheduled tasks.
The easiest way to do this is to open your WordPress dashboard, go to Plugins and click Add New. Once you’ve found the cron job plugin you want to use, click “Install Now”, wait a few seconds for the installation to finish, then tap “Activate”.
We recommend using the WP Crontrol plugin as it’s the easiest to use and unlikely to confuse less experienced users – it’s also free.
Once this plugin is set up and activated, you can go to “Tools” where you will now see the “Cron Events” and “Scheduled Actions” buttons, so let’s click on the first one. A new section will open where you can view your active cron events and schedules and add new ones.
To add a new event, you need to tap Add Cron Event, enter a hook name, add optional arguments (using the JSON-encoded array), select time intervals, and then click Add Event. Alternatively, you can go to “Add PHP Cron Event” and add your PHP code, after that everything will work the same as in the previous example.
What are the advantages of a cron job?
The ability to set up automatically scheduled jobs not only saves time but is also a great way to make sure you don’t forget important tasks. Also, by using cron jobs you have complete control over which jobs run as you can choose the exact time they run.
For example, if you run a membership site that has accounts with expiration dates, you can use cron jobs to periodically deactivate or delete expired accounts—giving you some time to focus on more important tasks.
Because cron jobs don’t take up any memory in your system when they’re not running, there aren’t any unexpected slowdowns either. If for some reason a cron job doesn’t run at the scheduled time, it will automatically start again when the next scheduled interval comes.
Choosing a cron job
Cron jobs are extremely effective at automating repetitive tasks, and setting them up isn’t too complicated, whether you use cPanel, Plesk, or WordPress.
So, after setting up your first cron job, it’s time for you to take a break while letting it do all those tedious tasks for you.