- Raspberry pi php exec
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
- Re: PHP — exec() not working — permission issue?
Raspberry pi php exec
I’ve setup lighttpd on my pi along with a couple other things including a bash script to use the google text-to-speech service.
This script is called ‘speech.sh’ and is stored inside the root dir of my webserver (/var/www/) along with my index.php
My index.php is as follows:
$json_string = file_get_contents("http://api.wunderground.com/api//geolookup/conditions/q/ll13%208pf.json"); $parsed_json = json_decode($json_string); $temp_f = $parsed_json->->; $weather = $parsed_json->->; $result = "Current temperature is $°C and it is $\n"; echo $result.'
'; exec('./speech.sh hello this is a test')
As you can see i’m using the wunderground weather API to get weather info and am looking to pass it to the speech.sh script.
The problem is, i can’t get exec to trigger the script at the moment.
I think this maybe a permissions or user issue from what i’ve read? Apparently the script will be being run by user ‘www-data’, rather than root (or maybe pi?) and so won’t be able to run ??
If this is the case, can anyone advise me on how i can either allow www-data user permission to run this script, or how i can get it to run as root from the exec command?
I can just about navigate my way though PHP but command line stuff and especially linux stuff i’m still very much a learner, so don’t know what i should be looking for here
SirLagz Posts: 1705 Joined: Mon Feb 20, 2012 8:53 am Location: Perth, Australia
Re: PHP — exec() not working — permission issue?
I’ve setup lighttpd on my pi along with a couple other things including a bash script to use the google text-to-speech service.
This script is called ‘speech.sh’ and is stored inside the root dir of my webserver (/var/www/) along with my index.php
My index.php is as follows:
$json_string = file_get_contents("http://api.wunderground.com/api//geolookup/conditions/q/ll13%208pf.json"); $parsed_json = json_decode($json_string); $temp_f = $parsed_json->->; $weather = $parsed_json->->; $result = "Current temperature is $°C and it is $\n"; echo $result.'
'; exec('./speech.sh hello this is a test')
As you can see i’m using the wunderground weather API to get weather info and am looking to pass it to the speech.sh script.
The problem is, i can’t get exec to trigger the script at the moment.
I think this maybe a permissions or user issue from what i’ve read? Apparently the script will be being run by user ‘www-data’, rather than root (or maybe pi?) and so won’t be able to run ??
If this is the case, can anyone advise me on how i can either allow www-data user permission to run this script, or how i can get it to run as root from the exec command?
I can just about navigate my way though PHP but command line stuff and especially linux stuff i’m still very much a learner, so don’t know what i should be looking for here
You’ll probably need to use the full path, or just the filename in exec. Rather than the ./ notation.
My Blog — http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
rpdom Posts: 22096 Joined: Sun May 06, 2012 5:17 am Location: Chelmsford, Essex, UK
Re: PHP — exec() not working — permission issue?
What are the current owner, group and permissions of the script?
Is it in the /var/www directory?
As Apache runs with user=www-data and group=www-data, setting the group of the script to www-data and adding group execute permissions should work (chmod g+x speech.sh).
Actually, I’m not sure that it is safe to have the script in /var/www. I think if apache has at least read access to it, someone could go to http://your-host/speech.sh and the script may be executed, or just displayed for them to read.
Re: PHP — exec() not working — permission issue?
Ok, so i’ve run `ls -la` on /var/www and this is the result.
I’ve chown’d the file a couple of times to change groups/users etc but i have no idea what i’m doing so have been running blind really.
Since posting the original question, i have also changed to apache2 if that makes any difference?
rpdom Posts: 22096 Joined: Sun May 06, 2012 5:17 am Location: Chelmsford, Essex, UK
Re: PHP — exec() not working — permission issue?
You will need to have something like group of www-data and -rwxr-x— permissions for your webserver to be allowed to run the script.
# Set group of file to www-data chgrp www-data speech.sh # Set permissions of file to -rwxr-x--- chmod 750 speech.sh # check permission (should look something like this) ls -l speech.sh -rwxr-x--- 1 pi www-data 911 Jun 30 20:41 speech.sh
Re: PHP — exec() not working — permission issue?
rpdom wrote: You will need to have something like group of www-data and -rwxr-x— permissions for your webserver to be allowed to run the script.
# Set group of file to www-data chgrp www-data speech.sh # Set permissions of file to -rwxr-x--- chmod 750 speech.sh # check permission (should look something like this) ls -l speech.sh -rwxr-x--- 1 pi www-data 911 Jun 30 20:41 speech.sh
Ok, so i got the same result as you in terms of permissions and group on the speech1.sh (changed filename since original post).
It’s still not working though
exec("speech1.sh this is a test");
exec("./speech1.sh this is a test");
and this works, so it’s something to do with the speech.sh not being accessed.
I created that speech1.sh file on the pi itself (via ssh) and so it was created by the pi user.
Would it make any difference if i created it on my mac and then ftp’d it into the root dir of my apache install (var/www/) ?
Also, does it make any difference that the script is in the var/www/ dir?? should it be outside this dir?
btw. just to confirm, i can run `./speech1.sh this is a test` from the command line and it works as it should, so it’s not the script itself that’s the problem.
Re: PHP — exec() not working — permission issue?
barryjarvis wrote: btw. just to confirm, i can run `./speech1.sh this is a test` from the command line and it works as it should, so it’s not the script itself that’s the problem.
A better test would be: sudo -u www-data ./speech1.sh this is a test
There was never anything wrong with the permissions. You do not seem to be checking the return status or getting any error message; you are just guessing that the exec fails. More likely it succeeds but the shell script does nothing. (Presumably there is no error checking in there either.)
At a guess, the script tries to make some noise. So it fails because user www-data is not in group audio.
Re: PHP — exec() not working — permission issue?
barryjarvis wrote: btw. just to confirm, i can run `./speech1.sh this is a test` from the command line and it works as it should, so it’s not the script itself that’s the problem.
A better test would be: sudo -u www-data ./speech1.sh this is a test
There was never anything wrong with the permissions. You do not seem to be checking the return status or getting any error message; you are just guessing that the exec fails. More likely it succeeds but the shell script does nothing. (Presumably there is no error checking in there either.)
At a guess, the script tries to make some noise. So it fails because user www-data is not in group audio.
SirLagz Posts: 1705 Joined: Mon Feb 20, 2012 8:53 am Location: Perth, Australia
Re: PHP — exec() not working — permission issue?
barryjarvis wrote: btw. just to confirm, i can run `./speech1.sh this is a test` from the command line and it works as it should, so it’s not the script itself that’s the problem.
A better test would be: sudo -u www-data ./speech1.sh this is a test
There was never anything wrong with the permissions. You do not seem to be checking the return status or getting any error message; you are just guessing that the exec fails. More likely it succeeds but the shell script does nothing. (Presumably there is no error checking in there either.)
At a guess, the script tries to make some noise. So it fails because user www-data is not in group audio.
My Blog — http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044
Re: PHP — exec() not working — permission issue?
The problem was with the www-data not being in the audio group. A quick `usermod` later and hey presto, it works
Re: PHP — exec() not working — permission issue?
barryjarvis wrote: The problem was with the www-data not being in the audio group. A quick `usermod` later and hey presto, it works
Hi, I have the exact same problem (but Im trying to use mpg123 alarm.mp3).
I followed all those www-data, «sudo usermod -a -G audio www-data», my command is executing «ls», but when I change to ‘mpg123 alarm.mp3’ still does work.
Could somebody give me more information ?!
Re: PHP — exec() not working — permission issue?
exec(«sh speech.sh «.escapeshellarg(«hello this is a test»));
txt3rob Posts: 368 Joined: Sat Aug 11, 2012 3:45 pm Location: Liverpool
Re: PHP — exec() not working — permission issue?
for me i put www-data in the sudo users and then set it to the only file it can run.
it prob needs root for some reason.
DougieLawson Posts: 42557 Joined: Sun Jun 16, 2013 11:19 pm Location: A small cave in deepest darkest Basingstoke, UK
Re: PHP — exec() not working — permission issue?
txt3rob wrote: for me i put www-data in the sudo users and then set it to the only file it can run.
it prob needs root for some reason.
That’s a massive security hole that you’ve opened there.
It’s much better to have a server (like pigpiod) that runs with root permission and a client (pigpio) that sends commands to the server.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors — are all on my foes list.
The use of crystal balls and mind reading is prohibited.
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- The MagPi
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Gaming
- Media centres
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Raspberry Pi 400
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Operating system distributions
- Raspberry Pi OS
- Raspberry Pi Desktop for PC and Mac
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion
- News
- Contact us
- Trademark
- About us
- Our Approved Resellers
- Jobs
- Accessibility
- Site use terms and conditions
- Acceptable use
- Cookies
- Licensing
- Terms and conditions of sale
- Privacy
- Security
- Verify our bank details