First what is a Cron Job?
A Cron Job is a Linux command for scheduling a command or script on your server to complete repetitive tasks automatically.
Scripts executed as a cron job are typically used to modify files or databases; however, they can perform other tasks that do not modify data on the server, like sending out email notifications.
For example, you can set a cron job to delete temporary files every week so that your disk space is not being used up by unnecessary files. Some scripts, such as Wordpress, may even require you to set up a cron job to perform certain functions.
Time Intervals
Cron jobs can be a very resourceful tool as they can be set to run by the minute, hour, day of the week, day of the month, month or any combination of these.
What You Should Know Before Using a Cron Job
You should have a working knowledge of Linux Commands before you use cron jobs effectively.
For most cron jobs, there are three components present:
The script that is to be called or executed
The command that executes the script on a reoccurring 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 instructions on what needs to be set up.
PHP
Below are some Example Cron Job Commands:
Command to run a PHP 8.0 cron job:
/opt/cpanel/ea-php80/root/usr/bin/php /home/username/public_html/cron.phpCommand to run a PHP 7.4 cron job:
/opt/cpanel/ea-php74/root/usr/bin/php /home/username/public_html/cron.phpOptional flags are sometimes required for a PHP cron job:
php -q /home/username/public_html/cron.phpCommand to GET a remote file:
/usr/bin/GET http://www.example.com/file.phpSome scripts executed with a Cron Job require a specific php.ini file to be used. When executing a PHP script from a Cron Job (or via the command line), you can use a custom php.ini file. The most common situation where a custom php.ini file is needed is when a user's code requires access to something such as Zend Optimizer specified in the php.ini file.
Command to use a specific php.ini file:
php -c /home/username/public_html/php.ini /home/username/public_html/myscript.phpIn this example, /home/username/public_html/php.ini is the full path to the php.ini file you'd like to use and /home/username/public_html/myscript.php is the full path to the php script you'd like to run.
Perl
Command to run a CGI cron job:
perl /home/username/public_html/cgi-bin/file.plSH
Command to run a code script cron job:
/bin/sh /home/username/public_html/file.shMySQL
Command to import a database:
mysql -u mysql_user -ppassword database_name < backup.sqlCommand to export a database:
mysqldump -u mysql_user -ppassword database_name > backup.sqlIt is a good practice to use the -p flag. The System will prompt for your password. This way your password stays secure and never exists on the server as plain text.