I have little problem. When I type in PHP on my Raspberry Pi machine this:
system("echo hello");
It prints to the web page. Why? Why it doesn't print into terminal?
I didn't see it on my PuTTY.
Thank you very much.
There is no problem with system. This function returns whatever the executed program printed to stdout. In this case it is "hello".
wall to print to everyoneAs Jasen suggested, you could use wall to print it to everyone using the system. This is useful if you don't know the number of the tty, but it displays a banner to the terminal if you don't run it as root.
system("echo test | wall");
To print manually, without the banner and root, you can add your apache server user to tty group, like so: (replace the user)
sudo usermod -a -G tty WEB_SERVER_USER
Then you could do something along those lines:
system("your command > /dev/pts/0");
or
$output = system("your command");
file_put_contents('/dev/pts/0', $output);
That of course assumes that your terminal is mapped to /dev/pts/0. ls /dev/pts/ to see the list.
stderrIn addition to the above, if your program outputs to stderr, you need to attach 2>&1 at the end of your command, for example:
system("your_command 2>&1 | wall");
This will redirect both streams.
You can also run your script in terminal, using php-cli. To install cli, use:
sudo apt-get install php5-cli
Then, in your script, you can use echo system("your_command 2>&1"); and run it in the terminal:
php script.php
ls /dev/pts/ show? Using putty should work.wall and see if it works. Try it with echo to be sure that the command actually outputs something to stdout (your might be outputting to stderr, it's a possibility).mesg does it say is y ?this is beacuse in a web context stdout is the result returned to the browser
if you want to write to terminals of logged in users
$wall=popen("wall","w");
fwrite($wall,"Hello!\n");
pclose($wall);
they must have "mesg" turned on. (I think on is the default)
the command mesg y will turn it on, man mesg for more info.
The system function does not executes the command in any attached tty, that is why don't see any output on the tty attached to your Putty session.
If you want to send commands to your GPIO pins in the Pi through PHP you can use the system function to do things like system("echo 1 > /sys/class/gpio/xyz) but be aware that php must be run with root privileges which isn't not by default in the Apache websever.
system— Execute an external program and display the output". Output ofecho helloishello, and you are surprised to see this displayed?