[PHP] Re: Ensure only one instance of a script is running
a more reliable solution: Write a lock file and update timestamp each minute or so ,(at varous places around the script) then in order to check if the script is already runninh, check the timestamp. Jeremiah Fisher [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Test for a lock file when the script starts, and remove it when the script ends. This isn't 100% reliable, but it's the typical solution to this problem. If the script fails, the lock file may not be removed (ever have a Mozilla browser crash and tell you the default profile is locked?), so be careful with your error handling! ?php // test for lock file if ( !$f = fopen( 'lock', 'r')) { // touch the lock file $f = fopen( 'lock', 'w'); fwrite( $f, 'lock'); fclose( $f); } else { // lock file exists; another instance must be running (we hope) echo 'Error: An instance of this script is already running!'; exit( 1); } /* script stuff */ // remove the lock file for the next instance unlink( 'lock'); ? Shaun wrote: Hi, I have a script that inserts data from files uploaded to our server. I need to make sure that only one instance of this script runs at anyone time, can anyone tell me how I can do this? Many thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Ensure only one instance of a script is running
Test for a lock file when the script starts, and remove it when the script ends. This isn't 100% reliable, but it's the typical solution to this problem. If the script fails, the lock file may not be removed (ever have a Mozilla browser crash and tell you the default profile is locked?), so be careful with your error handling! ?php // test for lock file if ( !$f = fopen( 'lock', 'r')) { // touch the lock file $f = fopen( 'lock', 'w'); fwrite( $f, 'lock'); fclose( $f); } else { // lock file exists; another instance must be running (we hope) echo 'Error: An instance of this script is already running!'; exit( 1); } /* script stuff */ // remove the lock file for the next instance unlink( 'lock'); ? Shaun wrote: Hi, I have a script that inserts data from files uploaded to our server. I need to make sure that only one instance of this script runs at anyone time, can anyone tell me how I can do this? Many thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Ensure only one instance of a script is running
Jay Blanchard [EMAIL PROTECTED] writes: [snip] I have a script that inserts data from files uploaded to our server. I need to make sure that only one instance of this script runs at anyone time, can anyone tell me how I can do this? [/snip] Don't run another instance. ba-dump ching! Ok, here is a quick and dirty way to do it, have the script check for the existence of a file containing a date and timestamp, if one does not exist the script creates one. If it does exist, the script exits. Once the script is nearing completion have it destroy the file (unlink). Unluckily, this will not prevent a race condition. The only file operations that are guaranteed to be atomic are mkdir() and symlink(). Try to create the directory and see if it fails. Also remember to rmdir() the lock directory before exiting -- Raj Shekhar Y! : Operations Engineer MySQL DBA, programmer and slacker Y!IM : lunatech3007 home : http://rajshekhar.net blog : http://rajshekhar.net/blog/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Ensure only one instance of a script is running
[snip] Unluckily, this will not prevent a race condition. The only file operations that are guaranteed to be atomic are mkdir() and symlink(). Try to create the directory and see if it fails. Also remember to rmdir() the lock directory before exiting How about grabbing the output from `ps -ax` and looping thourhgthe results to see if your script is being run? HTH, Mikey -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Ensure only one instance of a script is running
Mikey [EMAIL PROTECTED] writes: How about grabbing the output from `ps -ax` and looping thourhgthe results to see if your script is being run? Again the problem is of atomicity. This is a multi step process - do a ps -aux - do a grep on the output - start script processing What if second script between step 1 and 2 ? That is why we need something that is guaranteed to complete in 1 step. -- Raj Shekhar Y! : Operations Engineer MySQL DBA, programmer and slacker Y!IM : lunatech3007 home : http://rajshekhar.net blog : http://rajshekhar.net/blog/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php