tags:

views:

75

answers:

6

I'm looking for a good cronjob tutorial or book to learn how to create one using PHP.

+5  A: 

Cronjob is not something to create as Php process or script. Cron is a linux program that allows you to call a script at a regular interval.

You can see what is an crontab by entering in your linux machine as an admin user and type:

root@valugi:~# crontab -e

You will see something like

*/1 * * * * /usr/bin/php /var/www/somesite/public/cron.php

This means that each minute I am executing the cron.php.

Now, you may want to have different scripts executed at different times and want to pass this logic to php level instead of linux level. If this is the case you may want to call your cron script at the lowest time denominator (minute for example) and in the cron.php build some logic that will call at different times other scripts.

I use for example a Cronable interface:

interface Cronable {
    public function cron();
}

And each class that wants to be called by the cron.php has to implement this interface and the cron() function, which will specify what is the specific frequency of the call. The cron.php will get all this classes and will compare current time with that time and will decide to execute the call or not.

Elzo Valugi
Actually, the program is cron. crontab is the listing of jobs for it to run.
Ignacio Vazquez-Abrams
you are correct.
Elzo Valugi
A: 

You don't create one using PHP. You create one using crontab(1).

Ignacio Vazquez-Abrams
+1  A: 

i don't think you need a whole book to learn this.

basically you're just writing your script like you always do (avoid using $_SERVER) and the make an entry to your crontab like this:

* * * * * [/path/to/php/]php /path/to/your/script/script.php
oezi
+1  A: 

i start from reading this :

Writing and Automating PHP Crons in Windows and Linux

Haim Evgi
A: 

or you could just open CPANEL and look for CRONJOBS . there you can select what php file or script you need to run and when to do it, once per hour, per day, every sunday and so on .

voxberry
A: 

As others have said, cron is not something you'd call within PHP, which explains why you haven't found any tutorials for doing it.

The question is: what exactly are you trying to do? Do you want to run a PHP program on a regular interval? If that's what you want to do, then yes, you'll need cron, but nothing about it would be PHP specific, so just look for cron tutorials on how to format the crontab files.

If you want to configure your cron setup within PHP (which is how it reads from your question), then you'll need to modify the crontab file. This is a simple text file, so loading it and saving it would be as easy as any other text file. (You'll likely have issues with file permissions, but that's something you should probably get help with separately).

To modify the crontab settings, you'll need to know know how to format the entries in the crontab file, so again the same cron-specific tutorials I mentioned above will be useful (ie don't need to include 'PHP' in your search). Then write a php program that can save data in that format. It's actually not a very complicated format, so if you know PHP reasonably well, you should be able to work out how to parse the data. (but let us know if you need further assistance with that)

Hope that helps you get started.

Spudley