Running from CLI - Command Line Interface
Taking in consideration the amount of ressources needed to build a picture with PHP, it can be wise to
schedule the creation of the graphs on a periodic basis. Doing this way will prevent hanging the apache (
or whatever web server ) process. This documentation explains how to use PHP from the command line & how
to schedule a task from Windows or Linux ( using crontab )
Running PHP from the command line interface
PHP can be run from the command line interface of all operating systems. Putting the php binary folder in
your system path variable can be extremely wise!
To display the version of your PHP binary you can use the
php -v command.
Z:\>php -v
PHP 5.2.5 (cli) (built: Nov 8 2007 23:18:51)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
[ayashisunyday@pr-shellC ~]$ php -v
PHP 4.3.9 (cgi) (built: Feb 21 2007 06:35:52)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
To run a PHP script from the command line you can use the following syntax :
Z:\>php -q MyScript.php
You can also redirect the output of this command to a log file :
To run a PHP script from the command line you can use the following syntax :
Z:\>php -q MyScript.php >messages.log
Scheduling a PHP script from Linux
To schedule a job on linux ( assuming the crontab package is installed ) I recommand you to create a bash
file starting the PHP script and a cron task running this bash file. Doing this way you'll be able to change
the PHP stuff without modifying your cron job.
Create a text file called run with the following content :
/usr/local/php4/bin/php -q MyScript.php
/usr/local/php4/bin/php -q MyScript2.php
.. containing all of the PHP script you want to start. Then make this file executable :
chmod +x run
Now you can add this script in your crontab :
crontab -e
.. and put the following content :
30 04 * * * /path/to/run >>/var/log/php-scripts.log
.. and exit the text editor. The crontab will be installed on the system. You may need to restart the crontab daemon :
/etc/init.d/cron restart
.. or ( depending on the daemon you are using )
/etc/init.d/vixie-cron restart
Using
30 04 * * * will start the job every day at 04:30. to run a scrip every ten minutes you can use
10 * * * *. To have more infos on the crontab command
take a look here.
Scheduling a PHP script from Windows
To schedule a job on windows, I recommand you to create a batch file starting the PHP script and an at task running
this command file. Doing this way you'll be able to change the PHP stuff without modifying your task.
Create a text file called run.cmd with the following content :
php -q MyScript.php
php -q MyScript2.php
.. containing all of the PHP script you want to start. Now you can add this command file to your scheduled tasks :
at 20:30 /every:M,T,W,Th,F,S,Su run.cmd
This will start your task every day at 20:30. To have more infos dealing with the AT command you can take a look
on this :
How To Use the AT Command to Schedule Tasks.
You can also use the Windows graphical interface by going in the Control Panel & use the Scheduled task manager.
|
|
Last updated on 05/22/2008 |