Many of our clients don't have access to SSH on their shared hosting accounts, so they can't schedule cron jobs using command line. The good think is that CPanel is standard in the most of hosting packages and we can use it to perform Cron jobs scheduling.
What are Cron Jobs?
Cron jobs allow you to automate certain commands or scripts on a web site. You can configure a script or command to run at a specific time (e.g. every day, week...) For example, you could set a cron job to delete temporary files every week to free up disk space. Some scripts, such as Drupal, may require you to set up a cron job to perform certain functionality.
Note: You should have good knowledge and understanding of Linux commands before you can use cron jobs effectively.
For most cron jobs, there are three parts present:
- the script that is to be called or executed
- the command that executes the script on a reoccurring basis
- the action or output of the script, which depends on what the script being called does
Adding a cron job
1) To access the Cron Jobs Menu, click on the icon located in the Advanced section, on the main screen of your cPanel interface.
2) Enter the command you wish the cron job to run in the Command field.
3) Select how often you wish the command to be run by selecting an option in each of the boxes or by selecting a predefined period from the Common Settings drop-down menu.
4) This is optional step. Enter the email address where the cron output will be sent to in the corresponding field.
5) Click on Add New Cron Job to save your changes and set up your cron job.
Deleting a cron job
When a cron job is no longer needed, you should delete it so the command will not continue to run.
1) To access the Cron Jobs Menu, click on the icon located on the main screen of the cPanel interface
2) Click on the Delete button next to the cron job that you want to remove
What can be run as a cron job
Here are some examples that could be run as cron jobs
Command to import a database:
mysql -u mysql_user -ppassword database_name < backup.sql
Command to export a database:
mysqldump -u mysql_user -ppassword database_name > backup.sql
Command to run a PHP cron job:
php /home/%user_name%/public_html/cron.php
Command to GET a remote file:
/usr/bin/GET http://www.domain-name.com/file-name.html
Command to run a CGI cron job:
perl /home/%user_name%/public_html/cgi-bin/some-file.pl
Cron error output
You could redirect the errors output to a file of your choice by appending 2>/path/to/error.log to the end of your command. Since you can't view /var/log/cron on shared servers, this is the only way to see if there is an error when your script runs.