[SOLVED] Why does this bash script work if called from the command line but not when called from a php script run by a webpage?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What is displayed in the attached image is not the same html code as posted in the text since the image background is red versus the code which is pale green. It is impossible to say why it does not work.
As posted you really need to see the page source code as well as maybe the web server log files.
Personally I would use all php code and only call vcgencmd to get the temperature. Here is some quick code.
It seems some of the returned data from the bash script call is being returned to the calling PHP script but not the variable data that contains the actual numbers. That is what is throwing me off.
On a side note, I was mistaken in that the PHP script itself is NOT executable to the world, only the bash script (that is in another directory)
You will need to do some troubleshooting - comment out parts of it unitl you figure out where the data is "lost".
If you say the script works fine when called from a command line, I suspect that PHP doesn't have permission to execute these external commands at all.
Also, /opt/vc/bin/vcgencmd is a really opaque command to us, we don't know it at all.
You might need to configure PHP to give it permission to execute this command, or the command might need elevated permission to read the CPU temp values.
Assuming your server & PHP run things as the user www-data, you could try executing the script thusly:
I reproduced your code as much as possible, the output and colors do look as expected. The web browser is Chrome and the server is Apache.
Again check the server's error log and I would eliminate the bash script.
vcgencmd is a command line python utility that can get various pieces of information from the VideoCore GPU on the Raspberry Pi. An excellent point, as posted the web server runs as an unprivileged user which might not have permissions to run the vcgencmd.
It seems some of the returned data from the bash script call is being returned to the calling PHP script but not the variable data that contains the actual numbers. That is what is throwing me off.
Bash does not pick and choose what data to return - it is echoing what you have instructed it to.
(Also, you haven't posted a Bash script, because it doesn't start with "#!/bin/bash" and I doubt PHP explicitly uses Bash for its exec function.)
This means that either the juggling you are doing is clearing the values, or the original call to "/opt/vc/bin/vcgencmd measure_temp" is failing so they are always blank.
Start at the beginning and consider what the potential failures/deviations are at each stage, and how you can test/handle them - i.e. begin by trying to executing vcgencmd directly from PHP and see what it returns.
Once you've figured that out, go ahead and implement the rest in PHP, because it's likely to be significantly simpler there.
This works from the PHP call just fine. And while my problem is solved (and thanks to all who offered a suggestion), I'd be interested in figuring out why (never can have too much knowledge, right?)
And yes..my original bash script did have '#!/bin/bash' but I didn't include it in my original post
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.