PHP - Good Cronjob/Crontab/Cron Tutorial or Book

PHP - good cronjob/crontab/cron tutorial or book

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.

Creating a cron job in php

You can't. This is an OS level function not PHP level. Best bet is to email your host and ask them if it's possible to setup a cron for you.

Install a cron job with a php script

You just have to create the cron file, then use exec to set up that cron:

$cron_file = 'cron_filename';
// Create the file
touch($cron_file);
// Make it writable
chmod($cron_file, 0777);
// Save the cron
file_put_contents($cron_file, '* * * * * your_command');
// Install the cron
exec('crontab cron_file');

This requires that the user which PHP is run under has the right to make crontabs. This cron file will by default replace any other crons for that user, so make sure to ask the user if he wants to apply the cron. Also make sure the folder you're writing the crontab file in is writable.

Php run cronjobs

A cron job needs to be entered into the "cron" schedule to run at a particular time.

My guess is that there is a script in your installation which is normally started by cron and you want to start it when your php program detects some condition.

You need to find out where the crontab script is located (try "crontab -e" for a list of currently defineded cron jobs) and start it uing a php system() or exec() command.

How can I write a PHP cron script?

There are no problems running a PHP script as a cron job.
What you need to do is provide the full path in the filesystem to the script file, and use the command php to tell Linux what program to run the file with.

Example:

*/5 * * * * php /var/www/vhosts/statistikk/cron/getLastArticleFromIntranet.cron.php >> /var/www/vhosts/statistikk/cron/LOG.txt 2> /dev/null

This script will run every 5 minutes, all days of the week. Whatever the PHP-script echoes / prints out, will be stored to the LOG.txt file so that I can monitor the scripts events.

Try running just the command in your shell before putting it in the cronjobs to make sure it works.

However, you say that you normally call this script with a AJAX call. This will not be possible to do with a cronjob. I assume you use AJAX to pass along some $_POST-elements that the script needs. So what you have to do is either adapt the script to allow $argv parameters as well, and add them to the crontab job, or simply make a script which does not need any given parameters before it runs.

If you are going to adapt your script to support $argv parameters, follow the answer already existing on Stack Overflow about adding parameters to the job:

How to run a php url with parameters in cron tab

EDIT:

I'd just like to add to my answer as from the answer below.
To edit you crontab jobs in Linux you can simply use the command crontab -e.

This is the description of each required param that needs to be filled.

*     *     *   *    *        command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

Scheduled tasks (cronjob-aternative) with Zend framework?

Cron jobs really only offer a few basic benefits: scheduling, execution and logging. These are all things that are pretty easy to replicate in a PHP application...

Step One: Create a table of tasks

You'd need to store:

  1. Frequency of execution
  2. What to execute (include file, callback, eval code, etc.)
  3. Calculate next run date
  4. Store previous run dates

Step Two: Execution

You have a few options on how to actually trigger the tasks:

  1. Call a PHP-generated blank GIF image on every page run, which triggers the cron code.
  2. Call an AJAX script which runs the cron code
  3. Call it normally inside your application (may slow execution)

No matter how it starts, it would trigger the actual cron code, which decides whether or not there are any tasks to run, and which ones to run.

Step Three: Logging

This one should be pretty simple. Just log what happens during tasks to a file that you can read after to make sure its working.

...

Before running a task, you'd update the previous run date, and after running a task, you'd set the next run date, based on its frequency. The only fallback of this method is that when nobody visits the sites, no cron jobs will execute until the next visitor comes.

How to schedule in php

The Solution is very clear when you are using a Linux server;CRON JOBS.
One can easily run a cron job by configuring it through the terminal.I saw everyone has provided the Solution,but my answer will be for the people who are novice to Linux servers and don't know much about Cron Jobs.Go to Terminal and type the below commands..

root>which php

The above line will give you the path to where PHP is in your linux systems
Now,

root>crontab e

The above line will open the Cron file in edit mode.
Enter the number of times you want to run a particular php file and what time of the day,month,week,etc.
I am providing the syntex for running a particular file every 15 mins.
So here you go,

(write this in the cron file in edit mode)

*/15 * * * * path/to/your/php path/to/the/file/you/want/to/run

Now,path/to/your/php has to be replaced by the path what you got when you typed

root>which php

And you are done just save the file and close it.You will see a messege on you terminal that a new CronJob is installed.
That's it.



Related Topics



Leave a reply



Submit