0

I have a script running permanently from command line on a web server. There is a loop checking new stuff in DB. To save some load I need to add a condition not to check the DB if there is no reason. When the change occurs I need immediate reaction, however it might happen that there is no change for a few hours. And yes, it is not possible to do it in the webpage_script.php :)

The idea is to use some kind or a SUPERGLOBAL variable and in webpage_script.php save TRUE to that variable and then check it in that condition in permanently_running_script_on_the_same_server.php.

However I didn't find any variable that can be used for that reason... when I try to use session_id('whateverisinhereblahblab') - to share the session, the webpage_script.php will not get loaded as the session is probably continually occupied...

webpage_script.php

$the_shared_variable['whatever'] === FALSE;
if ($something_happens){
  $the_shared_variable['whatever'] === TRUE;
}

permanently_running_script_on_the_same_server.php

while (true) {
    if($the_shared_variable['whatever'] === TRUE){



    }
}
5
  • make $the_shared_variable a value from the database Commented Oct 16, 2021 at 19:50
  • Does the permanently running script do anything other than respond to the signal from the web script? If not, have the web script start it with shell_exec and run it to completion as a background task. See stackoverflow.com/q/2440023/14853083 Commented Oct 16, 2021 at 19:52
  • Tangentially Perpendicular - yes, this is just a marginal part of the job... Commented Oct 16, 2021 at 20:01
  • Lawrence Cherone - can you say a little bit more please... i guess you do not mean just select from DB and assign to a variable... is there any kind of premanent link from PHP to DB? Commented Oct 16, 2021 at 20:13
  • A super global is just a variable like any other, except that it's available in all scopes within your scrip. Setting a value in one script won't make it available in any other script. And the concept of sessions doesn't exist (or even make sense) when calling a script though CLI. You need to store the value somewhere (an in-memory cache like redis or similar might work). If it's possible, you could also trigger a cron job every nth seconds instead of running a while loop like that (since PHP isn't really optimized for having a "main loop"-like-style-script that). Commented Oct 16, 2021 at 20:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.