Reputation: 13
I have a process I want to execute (using exec()
), but I only want to execute it once. I tried using globals, but it didn't quite work out. This is what I have:
if ($started = false) {
$started = true;
exec("php something.php");
}
I don't want it to run only once when I load the page, but once the page has loaded one time and executed the process, it will never execute after that. So, how would I do that?
(I'm not sure if this is important, but the process includes a time_sleep_until
, so I can't really put something in that page after that or send back, I think..)
Upvotes: 1
Views: 90
Reputation: 2581
You could write a file after or before the process was executed. Next time you load your script you can check whether the file exists or not.
if (! file_exists('lockfile')) {
file_put_contents('lockfile', '1');
exec("php something.php");
}
Also you could create that file with content "0". If it is "0" you can execute your stuff and write "1" into the file. That way you could gain privileges(chmod) only to that file.
Of course you can also persist that kind of state in a MySQL, Redis, Mongo, ... This is just a choice of favor.
Upvotes: 1