Use of cron jobs

1

We consider that this message sometimes given by scripts and applications for the implementation of cron tasks: 'contact your server administrator' is worthy of the 80s :). In fact, whether it's the cPanel administration panel or another, this kind of elementary function has been made accessible to the end users, this is now a 'just do it' task, just read the basic cPanel instructions and this article if necessary...

From your cPanel, section Advanced / Cron jobs :

Gestion de Cron par cPanel


Attention :
Cron executing at too close intervals without need/justification leads to bugs, and overflow of resources leading to the malfunction of your website

According to your needs and recommendations of the editor, here are the 2 formats A and B in examples :
A. PHP scripts running in internal php :
/usr/bin/php -f /home/yourcpaneluser/public_html/yourfolder/your-sub-folder/yourfile.php >/dev/null 2>&1

or
php -f /home/your_user/public_html/yourfolder/your-sub-folder/yourfile.php >/dev/null 2>&1
(The '-f' flag tells php to parse/execute the following file)
(Check : http://php.net/manual/en/features.commandline.options.php

or, (if you get issues or forbidden access) :
/usr/local/bin/php -f /home/votreusercpanel/public_html/votredossier/votre-sous-dossier/votrefichier.php >/dev/null 2>&1

B. PHP scripts running externally :

wget -q -O /dev/null "http://www.yourdomain.com/yourfolder/your-sub-folder/yourfile.php" >/dev/null 2>&1
or
/usr/bin/wget -q -O /dev/null "http://www.yourdomain.com/yourfolder/your-sub-folder/yourfile.php" >/dev/null 2>&1
or

curl -s "http://www.yourdomain.com/yourfolder/your-sub-folder/yourfile.php" >/dev/null 2>&1

NB :
1. Adding this in cronjob is a good thing : 
sleep $(expr $RANDOM \% 35);
It helps server to not struggle with executing all cronjobs of servers at same second (ssh must be activated)... 
Integration example, it must be put at begin of cron like this :

sleep $(expr $RANDOM \% 35); /usr/bin/wget -q -O /dev/null "http://www.yourdomain.com/yourfolder/your-sub-folder/yourfile.php" >/dev/null 2>&1
2. Avoid putting all your cron with minute value all same at 0, also space the minutes value of your cron to avoid a local conflict of your application.
3. Adding >/dev/null 2>&1 permit to stop sending emails at each cron execution
4. For wget cron, adding  -q -O /dev/null permit to avoid wirting a file in your account at each cron execution


Specific scripts, and SHELL Scripts if ordinary cron don't work

A. cron with script sh
sh /home/yourcpaneluser/yoordomain.com/file.sh

B. Specificity of certain php script involving shell commands and/or shell_exec, in our virtualized environment using CageFs from Cloudlinux, it is necessary that you add this in your crontab though shell :

MAILTO="user@yourmail.com"
SHELL="/usr/local/cpanel/bin/jailshell"
PATH="/usr/local/bin/:/usr/bin:/bin"

And simply add cron in your cPanel :
* * * * * php -f /home/yourcpaneluser/yoordomain.com/cron.php >/dev/null 2>&1

NB : if needed, check well and adapt your php.ini settings in your cpanel, about the default disable_functions, read doc of CMS/module and/or ask editor.

Warning :
the -f option may not be appropriate for some scripts, such as when optimizing wordpress by disabling wp-cron.php in wordpress...

Disable wp-cron in wordpress by adding this in wp-config.php:
define ('DISABLE_WP_CRON', true);

and then adding the cron in cpanel (5 minutes schedule is recommended minimum, and a*update with your domain name):
/usr/bin/wget -q -O /dev/null "https://www.yourwebsite.com/wp-cron.php?doing_wp_cron"