Cron is a utility which schedules a command or script on the server to run automatically at a specified interval. A cron job is the scheduled task itself, a cron job can help automate recurring tasks.
Cron jobs can be set to run by the minute, hour, day of the week, day of the month, month or any combination of these. Cron jobs have three main components:
The script that is to be called or executed
The command that executes the script on a recurring basis (typically set in the cPanel)
The action or output of the script (which depends on what the script being called does)
Most scripts that require the use of a cron job will give you specific setup instructions. If you are unsure, check with the author of your script before adding a cron job.
To create a cron job
1. Log into cPanel
2. Under the 'Advanced' tab of cPanel, select the 'Cron Jobs' icon
3. 'Cron Email', make sure the current email address is valid. If not, enter a new, valid email and click Update Email. You will receive an email after the cron job has finished
4. Under Add New Cron Job, use the Common Settings drop-down menu to choose from a list of regularly used intervals; or set the frequency of your cron job by using the drop-down box next to each time unit. Common settings range from every minute to once a year
5. In the 'Command' field, enter the desired command
6. Select 'Add New Cron Job'
Your cron job will then execute a file at the specified time interval according to the settings you selected.
To remove or edit a cron job
1. Under the 'Advanced' tab of cPanel, select the 'Cron Jobs' icon
2. Scroll down to the last section called Current Cron Jobs
3. Find the cron job you wish to edit or delete
4. Under Actions, for the appropriate cron job, click either Edit or Delete
Below we've included some examples of cron job commands in order to further explain the options available.
Running a PHP script
Lets say your cpanel account username is accounta and your file is in public_html/somedir/task.php - then you would enter:
/usr/bin/php -q /home/accounta/public_html/somedir/task.php
This tells PHP to run your script quietly [-q] so that it doesn't echo anything to the cron scheduler. You have to put the full path to your script. All cPanel account home directories are at /home/[username]/ as above.
Running a wget command
The 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 http://www.mydomain.com/wp-cron.php?doing_wp_cron
Here the [-O /dev/null] switch tells wget to send any output to /dev/null, which is sending any output to the linux black-hole.
Running an executable script directly
Running a script as an executable is also possible. In this case you can simply enter the full path to your script. For example:
home/accounta/myscript.pl > /dev/null 2>&1
The file should have the correct permissions, usually it would require 755.
> /dev/null 2>&1
We will now break down the line above.
[>] This is the linux redirection symbol.
[/dev/null] As we have already covered is the linux black-hole, anything that goes here cannot get recovered.
[2>&1] The number 2 represents the error stream, so if your application produces an error, then these messages will show number 2. The following [>] redirection symbol then sends any errors generated to [&1] which is basically saying repeat the first argument again.
Cron jobs can be set to run by the minute, hour, day of the week, day of the month, month or any combination of these. Cron jobs have three main components:
The script that is to be called or executed
The command that executes the script on a recurring basis (typically set in the cPanel)
The action or output of the script (which depends on what the script being called does)
Most scripts that require the use of a cron job will give you specific setup instructions. If you are unsure, check with the author of your script before adding a cron job.
To create a cron job
1. Log into cPanel
2. Under the 'Advanced' tab of cPanel, select the 'Cron Jobs' icon
3. 'Cron Email', make sure the current email address is valid. If not, enter a new, valid email and click Update Email. You will receive an email after the cron job has finished
4. Under Add New Cron Job, use the Common Settings drop-down menu to choose from a list of regularly used intervals; or set the frequency of your cron job by using the drop-down box next to each time unit. Common settings range from every minute to once a year
5. In the 'Command' field, enter the desired command
6. Select 'Add New Cron Job'
Your cron job will then execute a file at the specified time interval according to the settings you selected.
To remove or edit a cron job
1. Under the 'Advanced' tab of cPanel, select the 'Cron Jobs' icon
2. Scroll down to the last section called Current Cron Jobs
3. Find the cron job you wish to edit or delete
4. Under Actions, for the appropriate cron job, click either Edit or Delete
Below we've included some examples of cron job commands in order to further explain the options available.
Running a PHP script
Lets say your cpanel account username is accounta and your file is in public_html/somedir/task.php - then you would enter:
/usr/bin/php -q /home/accounta/public_html/somedir/task.php
This tells PHP to run your script quietly [-q] so that it doesn't echo anything to the cron scheduler. You have to put the full path to your script. All cPanel account home directories are at /home/[username]/ as above.
Running a wget command
The 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 http://www.mydomain.com/wp-cron.php?doing_wp_cron
Here the [-O /dev/null] switch tells wget to send any output to /dev/null, which is sending any output to the linux black-hole.
Running an executable script directly
Running a script as an executable is also possible. In this case you can simply enter the full path to your script. For example:
home/accounta/myscript.pl > /dev/null 2>&1
The file should have the correct permissions, usually it would require 755.
> /dev/null 2>&1
We will now break down the line above.
[>] This is the linux redirection symbol.
[/dev/null] As we have already covered is the linux black-hole, anything that goes here cannot get recovered.
[2>&1] The number 2 represents the error stream, so if your application produces an error, then these messages will show number 2. The following [>] redirection symbol then sends any errors generated to [&1] which is basically saying repeat the first argument again.