Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 17 Jan 2010 12:01:48 -0800
From:      Nerius Landys <nlandys@gmail.com>
To:        Diego Montalvo <dmontalvo@gmail.com>
Cc:        freebsd-questions@freebsd.org, MaJ <majincline@gmail.com>
Subject:   Re: Running PHP File under Crontab...
Message-ID:  <560f92641001171201t37f5a123m3b59ff4ce6e007e3@mail.gmail.com>
In-Reply-To: <aefec1611001171028rc1cb284o5ca57bd67eed6c3d@mail.gmail.com>
References:  <aefec1611001171028rc1cb284o5ca57bd67eed6c3d@mail.gmail.com>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
> I am wanting to execute a PHP file 5 times a day via crontab. Is it
> possible? =A0If so what is the proper crontab command for this?

Hi.  I'm running several PHP programs via cron.


#1 Make sure you have CLI (command line interface) in your PHP port:

As root,

> cd /usr/ports/lang/php5
> make config
Then make sure the "CLI" is set to "on".  If it isn't, change it, and
recompile the port.  For example "portupgrade -f php5-5.2.12" will
recompile the port, if you have portupgrade installed.


#2 Write the PHP script you want to run.  There are different syntaxes
for writing a command-line PHP program but here is one of them:


<?php
// Your PHP code here
?>


Save this to a path "/path/to/mycode.php".


#3 Add cron job to execute this program.

Your crontab should look like this:

*/5 * * * * /usr/local/bin/php -f /path/to/mycode.php

(That would execute your PHP script every 5 minutes for example.)



That's it!

There is an alternate way to write PHP scripts for CLI, but I have not
used it extensively, so I don't know all the details or the correctest
way to do it.

You can write a script like this:

#!/usr/local/bin/php
<?php
echo "Hello!\n";
?>

And then save it to a file for example "test.php" and set the
executable permission on it.  Then you can just:

./test.php

from a terminal.  So you could change the cron to just execute the
script directly in this case instead of explicitly calling
/usr/local/bin/php in the crontab.



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?560f92641001171201t37f5a123m3b59ff4ce6e007e3>