Portal Home > Knowledgebase > cPanel > How to set up Cron Jobs in cPanel
Cron jobs allow you to automate repetitive tasks on the server that hosts your web site. This is a powerful tool that allows you to take a hands-free approach to repetitive tasks. For example, you could add a cron job that automatically copies a mySQL database to a separate location on your site as a backup.
Warning: You need to have a good knowledge of Linux commands before you can use cron jobs effectively. Check your script with your hosting administrator before adding a cron job.
To add or modify a cron job:
1. Click on the Cron Jobs link in the Advanced Menu area.
2. Enter the times for the cron job in the Minute, Hour, Day, Month, or Weekday fields. Refer to the following page to check exactly how to enter values in these fields - http://www.redhat.com/support/resources/tips/cron/cron.html
3. Enter the cron job script in the Command field.
Examples:
::> is Right:
env php -q /home/username/public_html/directoryname/emailfile.php
-----------------------
::> is Wrong:
env php -q http://domain.com/directoryname/emailfile.php
4. Click on the "Commit Changes" button. Your cron job has now been added or updated.
Note: You can always click on the "Reset Changes" button to change the cron job back to whatever it was before you started entering information.
==================================
Deleting a cron job:
You can delete any cron job at any time. If you are unsure what a cron job is doing it is safer to delete it and check the code with your hosting administrator.
To delete a cron job:
1. Click on the Cron Jobs link in the Advanced Menu area.
2. Click on the "Delete" button next to the required cron job.
========================================
For Python and Perl scripts there is no need to use full path to environment, environment interpreter directive will be enough on shared servers.
python /home/username/public_html/yourscript.py
perl /home/username/public_html/yourscript.pl
Alternatively you can just use curl request as cron job command for the same purpose:
curl “http://yourdomain.com/script.php?argument1=arg1&argument2=arg2”
curl “http://yourdomain.com/script.py”
curl “http://yourdomain.com/script.pl”
PHP
Command to run a PHP 5.5 cron job:
/opt/php55/bin/php /home/username/public_html/cron.php
---------
Command to run a PHP 5.4 cron job:
/opt/php54/bin/php /home/username/public_html/cron.php
--------
Command to run a PHP 5.3 cron job:
/opt/php53/bin/php /home/username/public_html/cron.php
--------
Command to run a PHP 5.2 cron job:
/opt/php52/bin/php /home/username/public_html/cron.php
--------
Optional flags are sometimes required for a PHP cron job:
php -q /home/username/public_html/cron.php
--------
Command to use a specific php.ini file:
php -c /home/username/public_html/php.ini /home/username/public_html/myscript.php
-------
CURL
curl "http://domainname.com/cron.php"
curl -s http://domainname.com/cron.php /dev/null
-------
GET
Command to GET a remote file:
/usr/bin/GET http://www.example.com/file.php
GET http://www.example.com/file.php
PERL
Command to run a CGI cron job:
perl /home/username/public_html/cgi-bin/file.pl
SSH
Command to run a code script cron job:
/bin/sh /home/username/public_html/file.sh
sh /home/username/public_html/file.sh
MySQL
Note: It is a good practice to not type your password out in the follow commands but to simply use the -p flag alone and have the system prompt you for the password. This way your password stays secure and never exists on the server as plain text.
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
Add to Favourites Print this Article