How to run scripts via cron jobs
- Category : cPanel Question
- Posted on : Mar 25, 2021
- Views : 795
- By : HostSEO
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 5 minutes or setting up more than 5 simultaneous cron jobs is not allowed on all shared servers.
2. If you do not add >/dev/null 2>&1 at the end of the command, the server will send an email notification each time a 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 the 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, the cron job is working.
In order to set up a cron job, go to cPanel > section Advanced > Cron Jobs menu:
PLEASE NOTE: Cron jobs are run according to the server time.
General cron job format is as follows:
[path to environment] [Path to script] [Script parameters]
For example, if you want to run a PHP script located in the public_html directory, the correct cron command will be:
/usr/bin/php /home/cPaneluser/public_html/yourscript.php
/usr/bin/php -q /home/cPaneluser/public_html/yourscript.php (for business servers)
or if you want to use a relative path (different commands are divided by “;”):
cd /home/YourcPuser/public_html/; /usr/bin/php yourscript.php
cd /home/YourcPuser/public_html/; /usr/bin/php -q yourscript.php (for business servers)
For Python and Perl scripts, there is no need to use a full path to the environment. The 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 the 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 the 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 the curl tool usage here.
You can also check if a cron command is executed correctly by running it in the command line via SSH.
We will use this simple summing script with external parameters and run it using a web browser, Cron and SSH:
First, let's just run it in the browser pulling arguments after the script name and “?” directive:
http://nctest.info/script.php?arg1=2&arg2=3
Now, in order to get the same result via cron we will use this command and add parameters after the script path:
/usr/bin/php /home/nctest/public_html/script.php 2 3
The email notification contains the same output:
In CLI, you can run both cron commands and CURL requests, just log into your cPanel account via SSH and input them like shown below:
This is it!
Categories
- cPanel Question 47
- cPanel Software Management 29
- cPanel Tutorials 13
- Development 29
- Domain 13
- General 19
- Linux Helpline (Easy Guide) 156
- Marketing 47
- MySQL Question 13
- News 2
- PHP Configuration 14
- SEO 4
- SEO 42
- Server Administration 84
- SSL Installation 54
- Tips and Tricks 24
- VPS 3
- Web Hosting 44
- Website Security 22
- WHM questions 13
- WordPress 148
Subscribe Now
10,000 successful online businessmen like to have our content directly delivered to their inbox. Subscribe to our newsletter!Archive Calendar
Sat | Sun | Mon | Tue | Wed | Thu | Fri |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
Recent Articles
-
Posted on : Sep 17
-
Posted on : Sep 10
-
Posted on : Aug 04
-
Posted on : Apr 01
Tags
- ts
- myisam
- vpn
- sql
- process
- kill
- tweak
- server load
- attack
- ddos mitigation
- Knowledge
- layer 7
- ddos
- webmail
- DMARC
- Development
- nginx
- seo vpn
- Hosting Security
- wireguard
- innodb
- exim
- smtp relay
- smtp
- VPS Hosting
- cpulimit
- Plesk
- Comparison
- cpu
- encryption
- WHM
- xampp
- sysstat
- optimize
- cheap vpn
- php-fpm
- mariadb
- apache
- Small Business
- Error
- Networking
- VPS
- SSD Hosting
- Link Building
- centos
- DNS
- optimization
- ubuntu