php - How to prevent multiples instances of a script? -


I have PHP scripts that I want to run on Linux as well as on Windows Server. Any changes for those two environments I want to use the same script without doing.

Theses script will be set with Cron (on Linux) and with Window Scheduler (or other, I do not care), for my Windows environment.

However, some theses script may take several minutes to complete. I just want to stop the script from launching the scheduler (one of the cron or windows), before it was started for the last time.

I'm not sure how to do this .. I'm sure that "lock" is issued when something goes wrong during execution, so it is launched next time without human intervention. .

Maybe move on a dummy file with a bunch, but I'm not sure how to do this

I also have a MySQL database on the Thousers server I thought the database Using a lock on the side.

  1- Start a transaction 2- Enter the name of the script in a table. 3- Script execution. 4- If successful, then delete the line and transaction or simply rollback;  

If the name of the script is in the table, then I can stop it from running if my script execution fails, MySQL will rollback the transaction automatically so that the line next time the script Do not appear on the name of

But, in one transaction, there is no way for other connections which is untold information? if so, how?

I also thought locking would be impossible if it is impossible to use rollback cheese.

  1- Enter the name of the script in a table if it does not already exist 2- Start a transaction 2- Select * From the table where the script_name 3-script execution for updates. 4- If successful, issue lock (rollback or commit)  

But my biggest problem is that with MySQL, choose updates until the last lock is released or 50 seconds timeout (innodb_lock_wait_timeout Variable) has expired. I would like to tell MyScall to the right place that my line is locked without affecting the entire data, the reason is that the innodb_lock_wait_timeout variable is a global one (not a session). Is there another variable to copy the NO_WAIT section available in Oracle?

Should I have to suspend the script 50 seconds without any problem?

What is the best way on that I am a php novice and I do not want to create any problems on the server.

Maybe I have another option I have not seen ..

I Solved this problem ... used sockets. If you can enable php_sockets extention, try this code here:

  $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); If (false === $ socket) {new exception throw ("can not create socket:" .socket_last_error ($ socket)); } ## Set $ ​​ports as something like 10000 ##, because the error will be checked manually if (false === @ socket_bind ($ socket, '127.0.0.1', $ port)) {## Some instance scripts are returning; } And {## let's do our work return the $ socket; }  

The specific operation on boundless socket on $ port is safe operation. The operation system will ensure that there is no other process that has a bound socket for the same port. You only need to check the return value.

If the script crashes then the Operation System will automatically stop the port.

It can be used in any language. I have done extensive testing on Pearl and PHP based projects. This parallel execution was stopped, even though we accidentally added scripts twice in the crontab twice.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -