Setup CRON Schedule Jobs
In this article you will learn to make a simple PHP script execute repeatedly at certain time interval.
To do this, it is assumed that you have access to a web hosting server with CPANEL and has CRON enabled. If it is not enabled for your account, contact your hosting service.
Before we setup Cron, let’s first prepare a PHP script with simple commands just for testing purpose.1234<?phpecho “\nTesting the Cron Scheduler”;echo "\nTodays date & time --> " . date("Y-m-d") . “ : “. date("h:i:sa");?>
Go to File Manager in CPanel. Copy the code and save it in a file with the name ‘myfirstcron.php’ in the path /home/youlogin/public_html. Now testing PHP script is ready. Let’s setup a schedule to execute once every 30 mins.
Schedule PHP script in CRON
You have to provide 3 inputs to complete a scheduled task:
- CRON Email – A Email Id which Server can intimate once the scheduled Cron job is completed successfully or whether the job encountered any error.
- Schedule – Specification about when the job has to be executed and at what interval. Minute, Hour, Day, Month, Weekday are the inputs here. Click on common settings that have parameters that are used more often. Some samples are provided here.
- 0 * * * * – Once Per Hour
- 0 0 * * * – Once per Day
- 0 0 * * 0 – Once per Month
- */30 * * * *-Once every 15 Mins
- Command – It is mostly a Unix Shell command that your server can run. In this article, since we are explaining about executing a PHP, enter the below command.
wget -O – http://mywebsite.com/myfirstcron.php
wget is not the only option to run a PHP. It can also run using ‘/usr/local/bin/php’, lunx, curl etc.,But the above mentioned option is quite enough to execute the PHP and let server to forward the output to your email along with other informational messages. This completes the scheduling of PHP script in CRON. Once you save this setting, keep on refreshing your email to check for the output. Once you are satisfied with the Output, do not forget to modify the Cron job setting, else it will keep on executing the PHP every 15 mins on all days and it will fill your email space.
Other Nice to Read References for wget
Previous Post: How to Backup Website data Automatically to Dropbox?
How to Schedule a PHP script in CRON from CPANEL?