I made a php cli script which is running on loop. I run it from server's terminal(windows). This server also function as a php webserver (xampp).

The php cli script is dealing with hardware i/o stuff (responding and giving logics to a microcontroller board through serial port). Which is always running.

And what i'm trying to accomplish is to make a web-based app (php cgi) to control that cli script. like sending some command to to make it do something.

What i've tried

I have tried using a kind of temporary json file. Which contents is generated by the cgi script.

Then the cli script read that file in every loops. And if there is a change in the json (a timestamp), the script use data inside json to do something accordingly. And then store that timestamp to compare it with the json for next loop.

But this cause a huge load on the server and the cli script become much slower. Which affects the responsiveness of the microcontroller.

the php cli loops is something like this.

<?php
$lastTimestamp=0;
while(true){
    //read json file
    $json=json_decode(file_get_contents("temp.json"));
    if($lastTimestamp < $json->stamp){
        //do something with $json->data
        ...
        //update $lastTimestamp
        $lastTimestamp = $json->stamp;
    }

    //rest of microcontroller logic here
    ...
}

and the temp.json file is something like

{
  "stamp": 1557653475,
  "data": {
    "nd_a": 1,
    "nd_b": 1,
    "nd_c": 0
  }
}

so the question is how to interract with the already running php cli script from the cgi script without using above methods? expecting a better way that is not affecting the server load and performance.

Edit: i also tried using database in place of json file, but the performance is still not good.

1 Answers

0
Wisam398 On

This way is inefficient. The simplest way is to make a cron job that executes the cli script. If you're using a linux system, here is how you setup a cron (on Ubunutu) https://help.ubuntu.com/community/CronHowto

Then in your cli script, you remove the loop and the job will read the temp.json (which is gonna be done every time the job runs) and compare both timestamps.

Now for keeping the $lastTimestamp I would suggest to create a text file right next to your cli script and store the value of $lastTimestamp in that file. Then only if there is a difference between the timestamp retrieved from the json and the timestamp in the text file, you overwrite the text file with the new timestamp.

You can use file_put_contents or fopen/fwrite to write into the file.

So your script would be like the following

<?php
$lastTimestamp=0;
$lastTimeStampFilePath = "lastTimeStampFile.txt";
//read text file
$lastTimestamp=file_get_contents($lastTimeStampFilePath);

    //read json file
    $json=json_decode(file_get_contents("temp.json"));
    if($lastTimestamp < $json->stamp){
        //do something with $json->data
        ...
        //update $lastTimestamp
        file_put_contents($lastTimeStampFilePath, $json->stamp);
    }

    //rest of microcontroller logic here

Maybe configure the job to run every 2 mins or 5 mins, depending on your requirements.