Cron Jobs run scheduled tasks at specific times. This is good to set up automated maintenance and other unmanned server duties.Unfortunately PHP does not provide any facility to run any script at any specified time or any specific interval.That’s why you need to know how to schedule a task in PHP with Cron Jobs.
There are mainly two methods to schedule a task in PHP-
1.Using Linux Commands-If you are using shared hosting with has Cpanel (Control Panel) then you can easily use this method.Follow these steps to schedule a task in PHP with Cron Jobs.
- Login to your cpanel.
- Click on the “Cron Jobs” icon inside cPanel, under Advanced.
- Select the Common Setting you want. This will automatically change the other time settings
- Alternatively, you may adjust the individual time settings. Minute, Hour, Day, Month and Weekday can be modified to achieve your goal.
- In the input box to the right of “Command”, type the name of the file type, then add a space and provide the path to the file which you would like the command to run.For example if you want to run a php file say cronjob.php then use following command-
Linux commandApache1php /home/USERNAME/public_html/cronjob.php
Here USERNAME is the user name which is provided by your web host provider for your website.
- Click the “Add New Cron Job” button.
2.Using Third Party websites-If your hosting provider does not provide facility for creating cron jobs then you can use third-party websites. There are few website which offers to create cron jobs for free.Few of them are mentioned below-
I)-SetCronJob-SetCron jobs provides facility to set cron jobs and also see their output.As a free user you have renew your account on every 30 days.You can create up to 50 task for a day.To sign up for SetCronJob go here.
II)-WebCron-WebCron also provides facility to set cron jobs and also see their output but only for a week for free user.An advantage of WebCron is that you run any script at 1 minute interval.Click here to sign up for WebCron.