LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 10:52 AM   #1
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 21

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:	37
Size:	10.1 KB
ID:	36993  

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

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

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 11:15 AM.
 
Old 08-21-2021, 11:54 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,764

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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 12:06 PM.
 
Old 08-21-2021, 06:26 PM   #4
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 21

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 06:30 PM.
 
Old 08-22-2021, 07: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, 08:04 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,764

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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 08:11 AM.
 
Old 08-22-2021, 08:12 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
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, 12:32 PM   #8
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 21

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 12:34 PM.
 
1 members found this post helpful.
Old 08-22-2021, 06:36 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
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, 01: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 01:10 AM.
 
Old 08-23-2021, 05:39 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,987

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



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 03: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 05:57 AM
[SOLVED] Why would a command work from the command line but not within a script? RandomTroll Programming 14 09-22-2016 11:49 PM
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM

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

All times are GMT -5. The time now is 10:45 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