So you've created a PHP script that does something like clean your tables or compiles a report on some data. You want it to run it every 2 hours but you don't want to go to your computer every 2 hours. What should you do? Cron job using crontab is what you should do. Cron job is basically setting a timer on your server to run the script at a certain time that you please. Sounds easy enough, but how do you do it? Lets find out!
The first thing you need to do is obviously create the PHP script. Before setting up the cron job test and see if the script produces the result you want. You don't want to schedule something and find out a year later that it wasn't doing anything.
Assuming you have that part laid out now you can move forward. Now to schedule a cron job just login to your server's admin control panel area and find the cron job/crontab setting. Here's where I found it in Plesk and Cpanel:
- Plesk: Domains > *domain_name* > Scheduled Tasks > *domain_user*
- Control Panel: Advanced Tools > Cron Jobs > Advanced
Try to play around if you couldn't find it as different versions and CPs name things differently. You'll know when you find it as it asks you for minute, hour, day, month, year and weekday.
The Cron Job Command
Let's talk about the command first. The command is the code to tell the server which code to run. There's four commands to choose from and you'll have to try them out to see which one works for you. Here are the commands.
wget -q -O /dev/null SCRIPT URL
fetch -o /dev/null SCRIPT URL
curl -s -o /dev/null SCRIPT URL
So for example if I want to run a backup code for this blog I would plug the following for the command.
The Time Setting
Now that you have the command down its time to tell the server when to run the PHP script automatically. So here are the options you can set:
Minute: 0 – 59
Hour: 0 – 23
Day: 1 – 31
Month: 1 – 12
Day of the Week: 0 – 7 (0 and 7 both represent Sunday)
Now these settings don't seem too flexible at all, but wait! there's more! There are some additional settings that can be used.
* - Run every x (x being the setting, * in days means run every day)
0,15,30,45 – Run every 0, 15, 30 and 45 of x
*/15 - Run every interval of 15 for x
10-15 - Run it every value between 10 and 15
I'm not sure if those settings are confusing or not. Here are some examples for you to better understand how the timing commands work.
0 0 */1 * * - Run every day
30 7 * * 2,4,6 - Run every Tues, Thurs and Sat at 7:30
0 10 */2 * * - Run every second day at 10:00
0 0 1 */1 * - Run on the first of every month
There you have it. Setting up your PHP script to run automatically even when you're not there.













Does this 0 */1 * * * mean run file once every 1 hour? juz wanna make sure!thanks!:D
@k: Hey K, that is correct!