📜 ⬆️ ⬇️

Utility to run php scripts in the background

Foreword


Perhaps you think that this is another article on how to fork processes with fork () via the PHP console. But alas, neither the console nor pcntl_fork () will be here - only a browser and server with PHP.

I wrote this quickly (I apologize in advance for the names of the variables) to this utility, for my own needs, and all because I did not find anything like it.
The meaning of this utility is the same - to run a script that will run all day in the background (as an option). Naturally, it can be done without any utilities there, but in this case we will slip at once several times in a row (many of these places know, but still, I repeat):

There are many more disadvantages of the “one file” approach, but the article is not about that.

Utility description


Why not use the console, pcntl_fork ()

When I wrote the utility, I expected to use it on different servers and in different conditions, in which it is not always possible to use the console (paid / free hosting) and in conditions when PHP is used as an Apache module (in this case, about pcntl_fork () you can forget).

Operating principle

The script is created / edited in the browser.
In case you need to work from a phone / tablet, or no FTP access to the server. Used wonderful editor ace (ajax.org).
')
Running script
To start, the browser “pulls” the file that connects the class to the beginning of the script and starts everything together for execution. Typical method to generate a new process.

The progress of the script
We can see all the messages that the script displays in real time.
To output messages to the console (improvised, in the browser window), the script must call the function
$PDT->display("", ""); 
After this, the browser receives a message about changes in the console file and updates the console in the browser, already with our message (it’s a long time to explain the mechanism, if it’s interesting to someone, I can write an article on this topic).

If you just specify an infinite loop in the script and start it, you can stop it only by stopping the process (which we cannot do with PHP). To avoid this situation, it is advisable to periodically use the function
 $PDT->running(); 
which returns false when you decide to stop the script and press the corresponding button in your browser.

If the script is designed to “wiretap” something (an infinite loop, until something changes), and there are no delays in it, then it will immediately load the processor through the outset. To avoid this feature is provided
 $PDT->wait(1000); 
which adds a pause in milliseconds (ms, in this example is 1 second) and sets the process status as “sleeping / waiting”.

Two scripts (and more) working in parallel can exchange data between themselves. Files are used for data exchange.
For example, the first script (first) called the function
 $PDT->write('', ''); 
and the second script (second), which listened on the “memory” of the first script, noticed the changes
 $var = $PDT->read('first.'); 
and changed something
 $PDT->write('first.', ' '); 

(this example is in scripts on GitHub)

The data in the script can be transmitted through the console. In this case, the script should listen for input
 $input = $PDT->input(); 


Note


The utility was written without the thought of going out into the world, for general use. Laid out just because of the thought that someone might come in handy.

Link to the utility (GitHub):
github.com/prineside/PDT

It is enough to throw all the contents into one folder and enter it through the browser.

Source: https://habr.com/ru/post/179285/


All Articles