Setting up CRON jobs

Cron is a task scheduler that is built into the Linux operating system. It is very flexible, and allows you to schedule all kind of automated tasks. The cPanel Cron jobs icon in the Advanced section of cPanel provides a simple graphical interface to using this great feature.

cPanel > Advanced > Cron Jobs

Cron email notifications

Email notifications for cron jobs will be sent to the accounts default email address.

You can change in the top section of the Cron Jobs screen:

Simply type in the email address you'd like cron notifications sent to and click 'Update Email'

Adding a Cron Job

You'll need to tell cron how often to run this task - you can select from Common Settings, to enter all the details - or for more specific control choose your own settings for Minute, Hour, Day, Month, Weekday

Then you'll add the command required to run the task. This will normally be in the format <executable> <switches> <task command file>

So for a PHP task your command might look like this:

/usr/bin/php -q /home/krystald/public_html/somedir/task.php

Where /usr/bin/php is the path to the PHP executable, -q is the 'quiet' switch - any output will just be sent straight to the trash, and finally the full path to the task command file.

Editing or deleting a cron job

Active cron jobs will be listed in the Current Cron Jobs section at the bottom of the page. You can Edit or Delete cron tasks here. Clicking Edit will allow you to change the time and task details, click the 'Edit Line' to save your changes. Clicking 'Delete' will show a confirmation 'Delete this cron job?' - click the 'Delete' button to confirm and the cron task will be removed.

Other examples

Running a wget command (great for firing off WordPress's wp-cron.php)

WordPress' wp-cron.php script can be activated by fetching the URL wp-cron.php?doing_wp_cron as follows:

/usr/bin/wget -O /dev/null https://www.mydomain/wp-cron.php?doing_wp_cron

Here, the [-O /dev/null] switch tells wget to send any output to /dev/null, which is linux-speak for saying send any output to the linux trash can, never to be seen again.

Ensure you replace mydomain.com with your own domain.

Running an executable script directly

Running a script as an executable is also possible, provided the script contains the correct shebang. In this case you can simply enter the full path to your script. For example:

/home/krystald/myscript.pl > /dev/null 2>&1

A few things to note here are :

The file must have the correct shebang entry at the top - on our servers,they are as follows:

#!/usr/local/bin/php

#!/usr/local/bin/perl

#!/usr/bin/python

#!/bin/sh

#!/bin/bash

The file should have the correct permissions - usually chmod 755 - however, if the file contains sensitive data and is outside of your public_html folder, then you should ensure it resides within a folder set to the nobody group. We can do this for you, but it's much more secure so let us know if you need this help.

> /dev/null 2>&1

What's this for, you ask? Let's break it down...

[>] this is the Linux redirection symbol - it basically says "send any messages that come out of the process on my left to the target on my right".

[/dev/null] as we have already covered is the Linux black-hole - the instant trash can.

[2>&1] The number 2 represents the error stream, so if your application produces an error, then these messages will squirt out of this number 2. The following [>] redirection symbol then sends any errors generated to [&1] which is a fancy way of saying repeat the first argument, or in other words [/dev/null] again.

So, basically, > /dev/null 2>&1 is a way of sending all output the script might generate to the trash.


How did we do?


© Krystal Hosting Ltd 2003–2019