0

More precisely, the variables in the script are not returned to the calling php script

First the call:

$CPUTemp = exec("/usr/local/sbin/supermon/get_temp");
print "   [ $CPUTemp]";

Now the bash script:

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:~#

This is what is displayed in browser This is what is displayed in browser

aynber
  • 22,380
  • 8
  • 50
  • 63
KenHorse2
  • 21
  • 1
  • 3
  • Does this answer your question? [getting output and exit status from shell\_exec()](https://stackoverflow.com/questions/1941755/getting-output-and-exit-status-from-shell-exec) – Amessihel Aug 20 '21 at 18:26
  • 1
    So it looks like it's accessing the bash script and returning something, so that part is working. Perhaps the user the web browser is running under does not have permissions for `/opt/vc/bin/vcgencmd measure_temp`? – aynber Aug 20 '21 at 18:28
  • Thanks for the edit (still a newbie here). but some of the bash script echo statements are being returned and vcgencmd is executable by the world. Just the variable parameters appear not to be – KenHorse2 Aug 20 '21 at 18:29
  • The script is undoubtedly trying to tell you what's wrong. You can find the error messages in your web server logs, or you can add `exec 2>>/tmp/mylog.txt` early in your script, refresh the page, and then look at the error messages in /tmp/mylog.txt – that other guy Aug 20 '21 at 18:35
  • Put exec 2>>/tmp/mylog.txt in right after the variable assignment block in the code and no file is created in /tmp. Nothing in apache2 error log either – KenHorse2 Aug 20 '21 at 18:43
  • Run `id` in the script and see what user it runs as. Then `su` to become that user in your Terminal and try running the script as him/her/them/it. – Mark Setchell Aug 20 '21 at 18:52
  • But the script runs when run by anyone (and is executable by anyone). What is the point of `id`? – KenHorse2 Aug 20 '21 at 19:43
  • Well..I've tried everything I can think of and still a no-go. No errors are reported either.... – KenHorse2 Aug 20 '21 at 20:45
  • Is SELinux involved? It may be protecting you from having your web server executing arbitrary scripts/executables. – Jeff Schaller Aug 21 '21 at 00:15
  • No it is not. This is a very straight forward Raspian Buster (Debian 10) platform – KenHorse2 Aug 21 '21 at 00:16
  • If it's a `bash` script, you could at the very least give it a proper shebang as the first line and make it executable! – Mark Setchell Aug 21 '21 at 18:52
  • Sorry. It does start with #! /bin/bash. I just didn't think I needed to include it as I mentioned it was a bash script...My bad – KenHorse2 Aug 22 '21 at 19:02
  • Dump the output of `vgencmd` like this: `CTEMP=$(/opt/vc/bin/vcgencmd measure_temp | tee /tmp/dump.txt)` and see if `exec` has problem with that command. –  Aug 24 '21 at 12:10

0 Answers0