Monday 25 July 2022

What is a cron job?

If you’re having a hard time remembering to run a script regularly or you simply wish to invest your time and energy into something a bit more creative, it’s high time to put cron to good use.

However, before diving in you’ll 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 there. Created in AT&T Bell Laboratories in 1975 and named after Chronos, a Greek personification of time, cron didn’t only come a long way from its initial inception but managed to remain relevant to this day.

Hostinger "free website" hosting offer: $41.88 $0
TechRadar Pro exclusive: Get everything you need to put your business online with a WordPress friendly hosting package that comes with 30GB of storage and 100GB bandwidth. Grab a full refund of $41.88/£41.88 in Amazon vouchers when you purchase Hostinger’s single shared hosting package. Terms and conditions apply. *Initial purchase required  View Deal

It’s commonly used for automating tasks that run in the background at specific times such as scheduled backups, monitoring disk space, managing system maintenance tasks, freeing drive space from useless files, sending bulk emails, and much more. All these different scheduled tasks (or jobs) are known as cron jobs.

To help you get started with cron jobs we’ll learn how you can set it up using cPanel, Plesk, and WordPress. However, before that, let’s find out a bit more about how a cron job works.

What is cron job used for?

Cron is essentially a daemon (a computer program that runs as a background process serving various purposes) that detects and executes cron jobs at specified dates and times whether it’s a one-time or recurring task. It runs every minute during which it inspects a set of pre-defined directories in your file system to determine whether cron jobs require to be run.

To run a cron job, three components need to be present including a specific command or a script that’s to be executed, a command that executes the script on a specific time basis (usually set in your control panel), and an action or an output of the script that would depend on what’s the script in question does.

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’ll be using crontabs, which is a list of commands OS uses to let you schedule all sorts of tasks that’ll be executed by cron expressions at the pre-set timing.

We should note that most scripts that use a cron job will come with specific how-tos, so you’ll know what needs to be set up and how.

How to set up a cron job using cPanel 

cPanel is the most popular Linux-based graphical user interface (GUI) out there and it will simplify the tedious tasks of managing and monitoring all aspects of your website or websites. So, if you have the pleasure of utilizing this user-friendly control panel, this is how you can set up a cron job with it.

First, let’s get started by logging into your cPanel. Once you’re 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 input your e-mail address so that the cron utility can send you e-mails every time one of the cron jobs produces an output – it’s a simple way to keep track of every job and review potential errors.

After this, you’ll want to scroll down to the “Common Settings” section and configure specifications about the cron jobs you want to add. Here you can pick the schedule you want a cron job to run on – once per minute, once per five minutes, once per hour, and so forth.

Once the schedule is chosen, the text box below will be filled out automatically, but you can alter it to match your needs.

At last, enter the command you want the system to run in the “Command” section, tap into “Add New Cron Job” and congratulations, you’ve set up a cron job.

How to set up a cron job using Plesk 

Plesk is a web hosting platform with a control panel that allows the administrator to set up websites, email accounts, DNS, and databases through 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 compatible with Linux servers only. On the other hand, Plesk supports both Windows and Linux servers.  

Much the same as with cPanel, to set up a cron job using Plesk you’ll need to log in and click on the “Scheduled Tasks” button on the right section of the dashboard.

There you’ll see an option to add, remove, and refresh tasks, but also a “Setting” section you’ll want to visit first and set a correct time zone. After this, click on “Add Task” and add any specification 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 has been set up.

The only slight difference between setting cron jobs using cPanel and Plesk is that with the latter one the icon doesn’t say “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’ll find inside the WordPress.org plugin library. This is part of WordPress’ own cron system which enables it to set and perform all sorts of scheduled tasks.

The easiest way to do this is to open your WordPress dashboard, go to “Plugins” and click on “Add New”. Once you find the cron job plugin you wish to use, click on “Install Now”, wait for a couple of seconds for the installation to be finished, and then tap into “Activate”.

We advise using WP Crontrol plugin since it’s the simplest one to use and not 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’ll now see “Cron Events” and “Scheduled Actions” buttons, so let’s click on the first one. It’ll open a new section where you can view your active cron events and schedules, as well as add new ones.

To add a new event you’ll need to tap into “Add Cron Event”, input a hook name, add optional arguments (using the JSON encoded array), choose time intervals, and then click on “Add Event”. Alternatively, you can go to “Add PHP Cron Event” and add your PHP code, after which everything works the same as in the previous example.

What are the benefits of a cron job? 

An ability to set up automatically scheduled jobs is not only a time-saver but also a superb way to make sure you don’t forget about critical tasks. Also, using cron jobs will give you complete control over what jobs are running, since you can choose the exact time they’ll be executed.

For instance, if you’re running a membership site where accounts come with expiration dates, you can utilize cron jobs to deactivate or delete expired accounts at regular intervals – which will give you some time off to focus on more important tasks.  

Also, since cron jobs aren’t occupying memory in your system when they’re not running, there won’t be any unexpected slowdowns. If a cron job fails to run at the scheduled time for whatever reason, it will automatically start again when the next scheduled interval comes.

Choosing a cron job

Cron jobs are extremely effective in automating repetitive tasks and setting them up isn’t too complicated regardless of whether you’re using cPanel, Plesk, or WordPress.

So, now that you’ve set up your first cron job it’s time for you to take a break while letting it do all those tedious tasks for you.

https://ift.tt/qWF3DMI

No comments:

Post a Comment