Run console command and read the return in PHP

Asked

Viewed 1,641 times

2

In linux if I do the command df -h in the terminal it returns me the partitions, size etc ... as shown below:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        25G  6.4G   17G  28% /
/dev/sda5        70G  2.9G   64G   5% /var
/dev/sda1       190M   29M  147M  17% /boot
/dev/sde1       394G  340G   34G  92% /u

You can take this result and bring it to php?

Example, save them to variables so I can display in a panel.

1 answer

4


The shell_exec() already executes and returns the output of the command as string:

<?php
   $output = shell_exec('df -h');
   echo "<pre>$output</pre>";
?>

Remember that the user account that is running PHP needs to have permission for the execution.

  • Right, but I can set which server I want, example: how would I do if it were 3 target servers ?

  • Each PHP will pick up from its own server. It runs as if it were a user.

  • Got it, Thank you ! I already got your answer, thank you !

  • @otaciojb if you need to run remote, you would have to use something like an ssh. You can do it with the same idea, but the complexity of the command increases a little.

  • is because I have 5 servers and would like to create a monitoring panel of them, a simple screen that shows this data. I’ll study the ssh question like you said, Thank you !

  • Think of a shell script that makes a Curl for your PHP with the data, it can be much simpler. Very few lines, and with CURL you can make the result of the command give a GET in a central PHP panel. There you don’t need to install anything. Just put it on the crontab,

  • Blza, Thank you !

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.