views:

67

answers:

4

I have cron job - php script which is called one time in 5 minutes. I need to be sure that previously called php script has finished execution - do not want to mix data that's being processed.

There are three approaches I used to apply:

  1. Creation of auxiliary text file which contains running-state flag. Executed script analyzes the contents of the file and breaks if flag is set to true. It's the simplest solution, but every time I create such script, I feel that I invented a bike one more time. Is there any well-known patterns or best-practices which would satisfy most of the needs?

  2. Adding UNIX service. This approach is the best for the cron jobs. But it's more time consuming to develop and test UNIX service: good bash scripting knowledge is required.

  3. Tracking processes using database. Good solution, but sometimes database usage is not encouraged and again - do not want to invent a bike, hope there is a good flexible solution already.

Maybe you have other suggestions how to manage single-processing of php scripts? Would be glad to hear your thoughts about this.

A: 

I'd stick to version number 1. It's simple and works out. As long as you only wan't to check whether the script has finished or not it should be sufficent. If more complex data is to be remembered I'd go for version 3 in order to be able to 'memorize' the relevant data...

hth

K

KB22
+1  A: 

I'd recommend using the file locking mechanism. You create a text file, and you make your process lock it exclusively (see php flock function: http://us3.php.net/flock). If it fails to lock, then you exit because there is another instance running. The advantage of using file locking is that if your PHP scripts dies unexpectedly or gets killed, it will automatically release the lock. This will not happen if you use plain text files for the status (if the script is set to update this file at the end of execution and it terminates unexpectedly, you will be left with untrue data).

Palantir
A: 

http://php.net/flock with LOCK_EX should be enough in your case.

Kane
+1  A: 

You could check wether or not your script is currently running using the ps command, helped by the grep command. "man ps" and "man grep" will tell you all about these unix/linux commands if you need informations about these.

Let's assume your script is called 'my_script.php'. This unix command :

ps aux | grep my_script.php

...will tell you if your script is running. You can run this command with shell_exec() at the start of your script, and exit() if it's already running.

The main advantage of this method is that it can't be wrong, where the script could have crashed, leaving your flag file in a state that would let you think it's still running.

Nicolas
Disadvantages: (1) it works unix-only (2) you must enable the exec or system functions, which may pose security risks or may be just not possible if you use hosted environment
Palantir
Very true. But you have to admit that asking the OS is more reliable than relying on a file updated by that very same process :)
Nicolas