Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
08-21-2021, 11:52 AM
|
#1
|
LQ Newbie
Registered: Dec 2004
Posts: 22
Rep:
|
Why does this bash script work if called from the command line but not when called from a php script run by a webpage?
More precisely, the variables in the script are not returned to the calling php script
First the call from the php script:
Code:
$CPUTemp = exec("/usr/local/sbin/supermon/get_temp");
print " [ $CPUTemp]";
Now the bash script:
Code:
echo -n "CPU: "
CTEMP=$(/opt/vc/bin/vcgencmd measure_temp)
CTEMP=${CTEMP:5}
SAVE=$IFS; IFS="'"; set -- $CTEMP; IFS=$SAVE
CTEMP=$1; FTEMP=$(echo 9 '*' $CTEMP / 5 + 32 | /usr/bin/bc)
if [ "$FTEMP" -le "120" ]; then
echo -en "<span style=\"background-color: palegreen;\">"
elif [ "$FTEMP" -le "150" ]; then
echo -en "<span style=\"background-color: yellow;\">"
else
echo -en "<span style=\"font-weight: bold; color: yellow; background-color: red;\">"
fi
echo -en " ${FTEMP}°F, ${CTEMP}°C </span> @ `date +%H:%M` "
If called from command line:
root@myhost:~# /usr/local/sbin/supermon/get_temp
CPU: <span style="background-color: palegreen;"> 88°F, 31.6°C </span> @ 08:07
root@myhost:~#
The attachment shows what is displayed in browser:
And yes, both scripts are executable by the world
Last edited by KenHorse; 08-21-2021 at 11:53 AM.
|
|
|
08-21-2021, 12:13 PM
|
#2
|
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,804
|
What the browser displays is far less helpful in diagnosing issues like this than what receives.
Press Ctrl-U in browser to view and copy the page source, or even better give the output of "curl -isS 'http://localhost/whatever.php'" instead.
Last edited by boughtonp; 08-21-2021 at 12:15 PM.
|
|
|
08-21-2021, 12:54 PM
|
#3
|
Moderator
Registered: Aug 2002
Posts: 26,336
|
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.
Code:
$ftemp=88;
$ctemp=31.6;
if ($ftemp<120) $backgd="palegreen";
echo"CPU:<span style=\"background-color: $backgd\";\">[ $ftemp°F, $ctemp°C </span> @". date("h:i:sa")." ]";
As an aside note php scripts called from your web server's document root should not be world executable.
Last edited by michaelk; 08-21-2021 at 01:06 PM.
|
|
|
08-21-2021, 07:26 PM
|
#4
|
LQ Newbie
Registered: Dec 2004
Posts: 22
Original Poster
Rep:
|
Quote:
Originally Posted by boughtonp
What the browser displays is far less helpful in diagnosing issues like this than what receives.
Press Ctrl-U in browser to view and copy the page source, or even better give the output of "curl -isS 'http://localhost/whatever.php'" instead.
|
Here is the result of the relevant section (I tried to clean up the formatting a bit):
Code:
[ CPU: <span style="font-weight: bold; color: yellow; background-color: red;"> °F, °C </span> @ 16:20 ]</p>
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)
Last edited by KenHorse; 08-21-2021 at 07:30 PM.
|
|
|
08-22-2021, 08:49 AM
|
#5
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
Quote:
Originally Posted by KenHorse
but not the variable data that contains the actual numbers.
|
Correct.
So you need to look at this part of the script:
Code:
CTEMP=$(/opt/vc/bin/vcgencmd measure_temp)
CTEMP=${CTEMP:5}
SAVE=$IFS; IFS="'"; set -- $CTEMP; IFS=$SAVE
CTEMP=$1; FTEMP=$(echo 9 '*' $CTEMP / 5 + 32 | /usr/bin/bc)
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:
Code:
sudo -u www-data /usr/local/sbin/supermon/get_temp
You should be getting the same erroneous output, hopefully with some additional error messages.
Next, add "set -x" to the top of your script.
You might also want to specifiy a shebang so that PHP knows which shell to use:
#!/bin/bash
at the very top.
|
|
|
08-22-2021, 09:04 AM
|
#6
|
Moderator
Registered: Aug 2002
Posts: 26,336
|
Code:
#!/bin/bash
CTEMP=$1; FTEMP=$(echo 9 '*' $CTEMP / 5 + 32 | /usr/bin/bc)
echo -n "CPU: "
if [ "$FTEMP" -le "120" ]; then
echo -en "<span style=\"background-color: palegreen;\">"
elif [ "$FTEMP" -le "150" ]; then
echo -en "<span style=\"background-color: yellow;\">"
else
echo -en "<span style=\"font-weight: bold; color: yellow; background-color: red;\">"
fi
echo -en " ${FTEMP}°F, ${CTEMP}°C </span> @ 12:24 "
echo
Code:
<?php
$cputemp=exec("/usr/local/bin/supermon.sh 50.5");
print " [ $cputemp]";
?>
Output:
Code:
[ CPU: 176°F, 80°C @ 12:24 ]
Page source:
Code:
[ CPU: <span style="font-weight: bold; color: yellow; background-color: red;"> 176°F, 80°C </span> @ 12:24 ]
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.
Last edited by michaelk; 08-22-2021 at 09:11 AM.
|
|
|
08-22-2021, 09:12 AM
|
#7
|
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,804
|
Quote:
Originally Posted by KenHorse
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.
|
|
|
08-22-2021, 01:32 PM
|
#8
|
LQ Newbie
Registered: Dec 2004
Posts: 22
Original Poster
Rep:
|
It was suggested to me by a local person that I try the following script:
Code:
#!/bin/bash
echo -n "CPU: "
CTEMP=`cat /sys/class/thermal/thermal_zone0/temp`
CTEMP=`expr $CTEMP / 1000`
if [ "$CTEMP" -lt "50" ]; then
echo -en "<span style=\"background-color: palegreen;\">"
elif [ "$CTEMP" -lt "60" ]; then
echo -en "<span style=\"background-color: yellow;\">"
else
echo -en "<span style=\"font-weight: bold; color: yellow; background-color: red;\">"
fi
FTEMP=`expr 9 '*' $CTEMP / 5 + 32`
echo -en " ${FTEMP}°F, ${CTEMP}°C </span> @ `date +%H:%M` "
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
Last edited by KenHorse; 08-22-2021 at 01:34 PM.
|
|
1 members found this post helpful.
|
08-22-2021, 07:36 PM
|
#9
|
Senior Member
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,984
|
Using the external expr program for integer arithmetics is old Bourne shell style.
The Posix standard defines $(( ))
Code:
CTEMP=$(( $CTEMP / 1000 ))
Code:
FTEMP=$(( 9 * $CTEMP / 5 + 32 ))
|
|
|
08-23-2021, 02:09 AM
|
#10
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
^ Yes, KenHorse, please make that small improvement.
Otherwise, well done!
Quote:
Originally Posted by KenHorse
I'd be interested in figuring out why
|
Please re-read my previous post, I speculated on the why. Wild guess: file reading/execution permissions.
Please mark your thread SOLVED now (see my signature). Others will benefit.
Last edited by ondoho; 08-23-2021 at 02:10 AM.
|
|
|
08-23-2021, 06:39 AM
|
#11
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,207
|
expr is deprecated - as it was already mentioned
backtick (`) is deprecated too, use $( ) instead - although probably not needed at all
additionally you can try shellcheck to check your shell script
|
|
|
All times are GMT -5. The time now is 11:51 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|