Powered by MediaWiki
Personal tools

Set up a Linux cron job

From b2evolution manual

Jump to: navigation, search

The following assumes you have your own Linux server with root access. Blindly following this procedure may lead to breaking your Linux setup. DO NOT do this if you don't understand it. Use this page only as a guideline.


You want to execute your .../cron/cron_exec.php script on a regular basis.

First you'll need to know exactly where this script resides on your system. You could use this command:

 locate cron_exec.php

(Note: if you have just uploaded the script it will not be found. In this case type updatedb to refresh your locate database).

Then you need to use the PHP interpreter in order to run cron_exec.php. Note: PHP comes in differents flavours: Apache module, CGI module, CLI... The preferred version to use here is CLI as in Command Line Interface. You could try this:

 /usr/bin/php -v

If you're set up correctly you should get something like:

 PHP 4.3.10-16 (cli) (built: Aug 24 2005 20:25:01)
 Copyright (c) 1997-2004 The PHP Group
 Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

Noticed the (cli) after the version number?

If you get an error, you may not have the PHP CLI package installed. Under DEBIAN, the package you want to install is php4-cli

If you get a lot of HTML, then you're running PHP but not the CLI version. This is not optimal but may work anyway.


Now try running the cron_exec from the command line:

 /usr/bin/php /your/specific/path/cron/cron_exec.php

On my system I got an arror about mysql needing to be enabled. This must be done in /etc/php4/cli/php.ini (again, location may vary, use locate php.ini)

Uncomment the following line:

 extension=mysql.so

Try again. When the script runs, it either outputs a lot of info about the scheduled job it executes, or when there is no job to execute, it simply says:

 There is no task to execute yet.




Now that you can run the script from the command line, let's automate it...

Edit the file /etc/crontab and add the following line:

 *  *    * * *   root  /usr/bin/php /your/own/path/cron/cron_exec.php >/dev/null 2>/dev/null

Note: root is the user under which the script will be run, you may want to change that. Also >/dev/null 2>/dev/null discards all messages and error messages, you may want to redirect those to a file for debugging or so...


[edit] Automating crons for multiple instances of b2evolution

This is for advanced users only.

If you are running multiple instances of b2evolution on the same server you may wish your system to automatically run new cron_exec scripts as you install them.

One solution would be to place this into /usr/local/bin/evo-cron:

 #!/bin/bash
 
 # Get list of crons we can run:
 CronList=`ls /home/*/www/cron/cron_exec.php`
 
 # Loop though all sites
 for Cron in $CronList
 do
   echo $Cron
   /usr/bin/php $Cron
 done

And call it like this in /etc/crontab:

 *  *    * * *   www-data  /usr/local/bin/evo-cron