PDA

View Full Version : Only allowing a script to run once at a time...


m002.p
06-26-2009, 08:07 AM
Hi everyone,

I have a script that runs every 3 seconds kickstarted by CRON every minute. I want to however stop the script overlapping itself as it sometimes does, so in short, I would like to only allow it to run once per time.

So I formulated a method without using MYSQL, a simple .txt file check as follows. The only issue is sometimes it forgets to remove the file so I end up with a script not running at all. In addition it places these files in the very root of the server not in the directory the script runs. Could this be something to do with it?

<?php
$fileN = "check.txt";
$filename = '/public_html/serverchecks/$fileN';

if (file_exists($filename)) {
exit();

}else{

$ourFileHandle = fopen($fileN, 'w') or die("can't open file");
fclose($ourFileHandle);

}

// Script code HERE

unlink($fileN);

?>

Any ideas?

Thanks in advance.

Matt

jchamber2010
06-27-2009, 01:46 AM
<?php
$fileN = "check.txt";
$filename = "/public_html/serverchecks/$fileN";

if (file_exists($filename)) {
exit();

}else{

$ourFileHandle = fopen($filename, 'w') or die("can't open file");
fclose($ourFileHandle);

}

// Script code HERE

unlink($filename);

?>

That should work now, you were using the wrong variable... I think, however I haven't time to test it, so hope that works for you.

Dismounted
06-27-2009, 04:53 AM
$filename = '/public_html/serverchecks/$fileN';
That is not going to work. This file never exists because the path is going to be literally read. As you are using single quotes, the variable is not substituted into the sting. Use double quotes or concatenate.