TCB13
TCB13

Reputation: 3155

PHP WebGUI to control CLI application

So, I've this scenario:

Embedded device running Debian, XAMPP and some CLI applications.

Those CLI applications are interactive, they update the data every second and the user can input commands to the terminal to change their behavior.

How can I build a WebGui in PHP/HTML start/stop/interact/read data from those applications without using a terminal? Should I use proc_open or exec? What's the best way to update the data without creating a CPU-killer loop?

Thank you.

Upvotes: 2

Views: 384

Answers (1)

Alexander
Alexander

Reputation: 1329

What do you mean by interactive applications? Do you send them commands with by calling from shell (like user@device$ application --stop) or do they offer own shell (like postgres or mysql CLI clients, etc.)?

Use exec() to send commands and read output:

$ls_output = exec('ls -l');

You may save continuing applications output by simply redirecting it to the file and read from this file with PHP when web-page is loaded. Add some javascript to automatically reload page, say, once in 10 seconds and it wouldn't be CPU-killing. Something like this:

user@device$ application --do-some-work > application_output

And in PHP:

$app_output = file_get_contents("application_output");

or even with GNU CLI tools and PHP exec():

$app_output = exec('tail -n 100 application_output | grep FAIL');

but it seems more like inventing bicycle, since you can filter output data in PHP.

Upvotes: 1

Related Questions