On Mon, Aug 18, 2008 at 4:44 AM, Hendra Susanto
<[EMAIL PROTECTED]> wrote:
> Hi, i am building a game using PHP. The game server needs to be run every
> minute. What i do now, is using cron job to run the server every minute.
> Another alternative, I am thinking of using loop, but the drawback is, it
> cannot be stopped whenever i want. Is there a better way to do this other
> than using CRON JOB and LOOP command? Is there a PHP command that can be
> time-triggered? Help please. Thanks in advance.

Hi Hendra,

I would suggest that your best method would be a CRON job.  There are
a few process control functions that you could look at which will
allow you to ask PHP to wait for so long (then you can check the time
and wait again) though these are not all documented and more than
likely will not be included in PHP by default.

If you are looking at running CRON jobs consider what tasks you need
to perform.  There are a few different methods you can employ, the
main two being
1) Separate scripts for each task run on their own CRON jobs; this
means that tasks can be run side-by-side if they need to and some
tasks can be easily run more or less frequently than others.
2) Run a management script that calls each of the tasks itself; this
keeps all of your code in one place and requires only one CRON job.
You can write it in a way which allows for some tasks to be run more
or less frequently, though this is harder to manage.  Without using
process control you won't easily be able to run multiple tasks at
once.

Also when writing CRON jobs that will run frequently consider using a
file on disk to indicate whether the task is already running.  At the
beginning of the script test to see if the file is present, if not
create it and get on with the task, if so e-mail yourself (perhaps?)
to let you know that the process is still running from last time.  At
the end of the successful run delete the file.  This helps you to
prevent the same task from being run while it's still finishing the
last run.

My final piece of advice is to make sure you run your CRON jobs as the
same user as your web daemon (on dedicated hosting this is often
apache, on shared hosting it is normally your own user).  You don't
want to start getting into cross-user permission issues ;-)

Hope this helped?

Phill Sparks
milk-hub.net

Reply via email to