Why does this bash script work if called from the command line but not when called from a php script run by a webpage?

KenHorse

New Member
Joined
Mar 12, 2021
Messages
21
Reaction score
6
Credits
176
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
 

Attachments

  • CPUTemp.JPG
    CPUTemp.JPG
    10.1 KB · Views: 195


Most likely because the shell lacks the required information to execute properly as it didn't start as if someone logged in to the shell.

So, add to the bash script the path to the user's profile you want to execute as.

If you are running it as root (if absolutely necessary) then add...

Code:
. /root/.bash_profile

Ensure the path to the profile is correct and the filename is correct. Yes, the line starts with a period then a space before the path. ".<space>/path/to/profile"

The user's profile contains path information required to find the commands required within the script.
 
Are you saying that user is what the webserver runs as (apache2, usually user www-data)?
 
If Apache is running this, then it's likely it won't have access to root's profile. So, you likely cannot execute it as root from the Apache user.

What you might do is execute the script via cronjob and write the output to a file that is readable by the apache user, then inject that text into the html.

If you want to continue to try to run it in that fashion, add what I said above to the bash script for a user profile that Apache user can reach.
 
If permissions are the issue, then why can I run the bash script as any user (as I said, it's executable by anyone). Also, why would all but the variables be displayed by the php script on the webpage. For example, "CPU" and the red background in the F and C degree fields? That part is running, yes?
(not being combative, I'm just trying to understand why is going wrong, other than my knowledge of Linux :oops:)
 
When you execute that script from Apache, it's being executed as the apache user. When it executes, it doesn't set your profile variables, so YOU must set them within the bash script by executing a profile script.

Either that or you can directly call each executable in your script with absolute paths. For instance, /bin/echo
 
I see. But why do the non-variable assigned parts of the echo statements work?

Code:
echo -n "CPU: "
echo -en "<span style=\"font-weight: bold; color: yellow; background-color: red;\">"
echo -en "&nbsp;${FTEMP}&deg;F, ${CTEMP}&deg;C </span>&nbsp; @ `date +%H:%M`&nbsp;"

And I do know that apache2 runs as user "www-data" (this is Debian Buster by the way) but that is a non-login user so there is no bash profile to be had, yes?
 

Members online


Top