LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 08-21-2021, 11:52 AM   #1
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 22

Rep: Reputation: 1
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 "&nbsp;${FTEMP}&deg;F, ${CTEMP}&deg;C </span>&nbsp; @ `date +%H:%M`&nbsp;"
If called from command line:

root@myhost:~# /usr/local/sbin/supermon/get_temp
CPU: <span style="background-color: palegreen;">&nbsp;88&deg;F, 31.6&deg;C </span>&nbsp; @ 08:07&nbsp;

root@myhost:~#

The attachment shows what is displayed in browser:

And yes, both scripts are executable by the world
Attached Thumbnails
Click image for larger version

Name:	CPUTemp.JPG
Views:	40
Size:	10.1 KB
ID:	36993  

Last edited by KenHorse; 08-21-2021 at 11:53 AM.
 
Old 08-21-2021, 12:13 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,804

Rep: Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690

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.
 
Old 08-21-2021, 12:54 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,336

Rep: Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146
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\";\">[&nbsp;$ftemp&deg;F, $ctemp&deg;C </span>&nbsp; @". date("h:i:sa")."&nbsp;]";
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.
 
Old 08-21-2021, 07:26 PM   #4
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 22

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by boughtonp View Post
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;">&nbsp;&deg;F, &deg;C </span>&nbsp; @ 16:20&nbsp;]</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.
 
Old 08-22-2021, 08:49 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by KenHorse View Post
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.
 
Old 08-22-2021, 09:04 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,336

Rep: Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146Reputation: 6146
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 "&nbsp;${FTEMP}&deg;F, ${CTEMP}&deg;C </span>&nbsp; @ 12:24&nbsp;"
echo
Code:
<?php
$cputemp=exec("/usr/local/bin/supermon.sh 50.5");
print " &nbsp; [ $cputemp]";

?>
Output:
Code:
 [ CPU:  176°F, 80°C   @ 12:24 ]
Page source:
Code:
&nbsp; [ CPU: <span style="font-weight: bold; color: yellow; background-color: red;">&nbsp;176&deg;F, 80&deg;C </span>&nbsp; @ 12:24&nbsp;]
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.
 
Old 08-22-2021, 09:12 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,804

Rep: Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690Reputation: 2690
Quote:
Originally Posted by KenHorse View Post
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.

 
Old 08-22-2021, 01:32 PM   #8
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 22

Original Poster
Rep: Reputation: 1
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 "&nbsp;${FTEMP}&deg;F, ${CTEMP}&deg;C </span>&nbsp; @ `date +%H:%M`&nbsp;"
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.
Old 08-22-2021, 07:36 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,984

Rep: Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273
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 ))
 
Old 08-23-2021, 02:09 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ Yes, KenHorse, please make that small improvement.

Otherwise, well done!

Quote:
Originally Posted by KenHorse View Post
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.
 
Old 08-23-2021, 06:39 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,207

Rep: Reputation: 7684Reputation: 7684Reputation: 7684Reputation: 7684Reputation: 7684Reputation: 7684Reputation: 7684Reputation: 7684Reputation: 7684Reputation: 7684Reputation: 7684
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why do 'bash -c <shell script>' and 'exec <shell_script>' work fine on the command line, but not when used for custom app launcher? bcsm Linux - General 6 08-10-2021 04:17 PM
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 06:57 AM
[SOLVED] Why would a command work from the command line but not within a script? RandomTroll Programming 14 09-23-2016 12:49 AM
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 01:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration