If you want to run scripts on a scheduled basis keep in mind the following:

1) According to our Acceptable Use Policy running cron jobs with intervals of less than 15 minutes, or setting up more than 5 simultaneous cron jobs is prohibited.

2) If you do not add >/dev/null 2>&1 at the end of the command, the server will send email notification each time cron job runs. While it may be useful for testing purposes and monitoring, if not attended, such emails will be accumulating in the email account specified in Cron email field and if it is one of your cPanel email accounts – they might cause issues with inodes usage.

NOTE: if your script does not generate any output, it is possible that notification emails are not sent either, however cron job is working.

In order to setup cron job go to cPanel > section Advanced > Cron Jobs menu

cron

General cron job format is as follows:

[path to environment] [Path to script] [Script parameters]

For example, if you want to run PHP script located in public_html directory, the correct cron command will be:

/usr/local/bin/php /home/cPaneluser/public_html/yourscript.php

or if you want to use relative path (different commands are divided by “;”):

cd /home/YourcPuser/public_html/; /usr/local/bin/php yourscript.php

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/YourcPuser/public_html/yourscript.py

perl /home/YourcPuser/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”

However unlike first method, curl-based cron jobs will work only if URLs specified in curl requests are resolving, e.g. DNS records for the domain are correct and running.
You can find more information about curl tool usage here: http://curl.haxx.se/docs/manual.html

You can also check if cron command is executed correctly by running it in command line via SSH.

We will use this simple summing script with external parameters and run it using web-browser, Cron and SSH.

 

cron4

First lets just run it in browser, pulling arguments after script name and “?” directive.

http://nctest.info/script.php?arg1=2&arg2=3

cron1

Now in order to get the same result via cron we will use this command and add parameters after the script path:

/usr/local/bin/php /home/nctest/public_html/script.php 2 3

Email notification contains the same output:

In CLI you can run both cron commands and CURL requests, just log in to your cPanel account via SSH and input them like shown below:

This is it!

 

Need any help? Contact our HelpDesk

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *