Jixit
asked on
Prevent Cronjob PHP script to run simultaneously with another PHP script
Hi,
I have a problem where clients are paying for a membership and while that script is processing the payment, a cronjob kicks in at the same time. This results in an unwanted duplicate transaction. Is there a way to prevent the cronjob from running, while the other script is still running?
Hope someone can help.
Jikke
I have a problem where clients are paying for a membership and while that script is processing the payment, a cronjob kicks in at the same time. This results in an unwanted duplicate transaction. Is there a way to prevent the cronjob from running, while the other script is still running?
Hope someone can help.
Jikke
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi thanks for your comments! I will use the databse mechanism like you've explained! Thanks a lot for your help.
Using the database, SQL code:
setup:
CREATE TABLE CronLock (locked int);
set lock:
INSERT INTO CronLock set locked=1;
unlock:
DELETE FROM CronLock;
check for lock:
SELECT * FROM CronLock;
Using a file, PHP code:
set lock:
$fh = fopen('lockfile.txt','w');
fwrite($fh,'locked');
fclose($fh);
unlock:
unlink('lockfile.txt');
check for lock:
$fh = fopen('lockfile.txt','r');
if($fh) echo 'locked!';
else echo 'open';