Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - good cronjob/crontab/cron tutorial or book [closed]

Tags:

php

cron

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

like image 345
HELP Avatar asked Oct 18 '10 12:10

HELP


2 Answers

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.

like image 60
Elzo Valugi Avatar answered Nov 17 '22 12:11

Elzo Valugi


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
like image 2
oezi Avatar answered Nov 17 '22 12:11

oezi