If you find it difficult to script regularly or if you just want to put your time and energy into something more creative, then it’s time to put cron to good use.
But before you dive in, 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 it’s one of the most useful utilities you’ll find out there. Created at AT&T Bell Laboratories in 1975 and named after Chronos, a Greek personification of time, cron has not only come a long way since its inception, but has managed to remain relevant to this day.
It is commonly used to automate tasks that run in the background at specific times, such as scheduled backups, monitor disk space, manage system maintenance tasks, free up disk space from useless files, send bulk emails, and much more. All these different scheduled tasks (or jobs) are known as cron jobs.
To get you started with cron tasks, let’s learn how to set it up with cPanel, Plesk, and WordPress. First, though, let’s learn a little more about how a cronjob works.
What is cronjob used for?
Cron is essentially a daemon (a computer program that runs as a background process that serves several purposes) that detects and executes cron tasks on certain dates and times, be it a one-time or recurring task. It runs every minute during which it inspects a series of predefined directories in your file system to determine whether to run cron tasks.
In order for a cron job to run, three components must be present, including a specific command or script to run, a command that runs the script on a specific time basis (usually set in your control panel), and an action or an output from the script that would depend on what the script in question is doing.
Cron jobs can be scheduled to run multiple times a day or to run at specific hours and minutes on specific days and months. To do this, you use crontabs, a list of commands the operating system uses to let you schedule all kinds of tasks that cron expressions will run at the preset timing.
We should keep in mind that most scripts that use a cron job have specific instructions so you know what to set up and how.
Set up a cron job with cPanel
cPanel is the most popular Linux based graphical user interface (GUI) out there and it simplifies the tedious tasks of managing and monitoring all aspects of your website or websites. So, if you have the pleasure of using this easy-to-use control panel, this is how you can set up a cron job with it.
First, let’s start by logging into your cPanel. Once you are in the cPanel dashboard, scroll down to the “Advanced” section, search 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 that the cron utility can send you emails whenever one of the cron jobs produces an output – it’s an easy way to do any job track and review potential errors.
After that, you’ll want to scroll down to the “General Settings” section and configure specs about the cron tasks you want to add. Here you can choose the schedule you want a cron job to run on – once every minute, once every five minutes, once every hour, and so on.
Once the schedule is chosen, the text box below will automatically populate, but you can customize it to suit your needs.
Finally, enter the command you want the system to execute in the “Command” section, tap “Add new cron task” and congratulations, you have set up a cron task.
Setting up a cron job with Plesk
Plesk is a web hosting platform with a control panel that allows the administrator to set up websites, email accounts, DNS and databases via a web browser.
While Plesk and cPanel serve the same purpose, they differ when it comes to GUI and the servers they support. cPanel’s intuitive interface is more user-friendly, but the web-based control panel is only compatible with Linux servers. On the other hand, Plesk supports both Windows and Linux servers.
Much the same as with cPanel, to set up a cron job with Plesk, you need to log in and click the “Scheduled Tasks” button on the right side of the dashboard.
There you will see an option to add, remove and refresh tasks, but also a “Setting” section that you want to visit first and set a correct time zone. After that, click “Add Task” and add any specification about the cron task you want to run, including time intervals, notifications, a description and enter the actual command. That’s it, the cronjob is set up.
The only minor difference between setting up cron jobs with cPanel and Plesk is that the latter doesn’t have the icon saying “cron jobs”.
How to set up a WordPress cron job
If you want to set up a cron job for your WordPress website, the first step is to install and activate one of the cron job plugins (such as WP Crontrol, Easycron and Advanced Cron Manager), which you have in the WordPress website finds. .org plugin library. This is part of WordPress’ own cron system that allows it to set up and run all kinds of scheduled tasks.
The easiest way to do this is to open your WordPress dashboard, go to “Plugins” and click “Add New”. Once you find the cronjob plugin you want to use, click “Install Now”, wait a few seconds for the installation to complete, then tap “Activate”.
We recommend using the WP Crontrol plugin as it is the easiest to use and is likely 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), choose time intervals and then click “Add Event”. You can also go to “Add PHP Cron Event” and add your PHP code and everything will work the same as in the previous example.
What are the benefits of a cron job?
The ability to set up automatically scheduled tasks is not only a time-saver, but also a great way to make sure you don’t forget about critical tasks. Also, using cron tasks gives you full control over which tasks are run as you can choose the exact time they run.
For example, if you have a membership site where accounts have expiration dates, you can use cron tasks to deactivate or delete expired accounts at regular intervals, freeing up some time to focus on more important tasks.
Since cron jobs don’t take up memory in your system when they’re idle, there won’t be any unexpected slowdowns either. If for any reason a cron job doesn’t run at its scheduled time, it will restart automatically when the next scheduled interval comes.
Choose a cron job
Cron tasks are extremely effective at automating repetitive tasks and setting them up isn’t too complicated whether you’re using cPanel, Plesk or WordPress.
So now that you’ve set up your first cronjob, it’s time to take a break while letting it do all those tedious tasks for you.