Posts: 4
Joined: Mon Feb 04, 2013 3:38 pm

executing shell script from php

Mon Feb 04, 2013 3:52 pm

I've been trying to port an X10 control program from a Fedora distro to the Raspberry and I'm having trouble executing the shell script using php.

The web page, x10b2.php, creates a shell script, x10b2.sh, and then tries to execute it using this command:

Code: Select all

echo shell_exec('sh x10b2.sh');
the shell_exec returns nothing and nothing happens on X10 device. But when I try to execute the shell script from a command line, the script runs perfectly.

the shell script:

Code: Select all

/home/pi/heyu-2.10/heyu on A12
Everything resides in the /var/www/x10 directory which is owned by www-data:www-data and the permissions are all set to 755. Pi is a member of the www-data group.

Does anyone have any suggestions about what might be the problem?


Posts: 281
Joined: Sat Feb 02, 2013 12:41 pm
Location: Berkshire, UK

Re: executing shell script from php

Mon Feb 04, 2013 10:23 pm

The PHP manual for shell_exec states that NULL isreturned on erro, so the fact you see nothing looks to be correct!

I'd guess that the environment when your scripts runs within PHP in a browser is not what you expect. Try putting the full path for your script in the shell_exec command, so something like:
echo shell_exec('sh /var/www/x10/x10b2.sh');

And if that fails try fully qualifying sh as well, as in:
echo shell_exec('/bin/sh /var/www/x10/x10b2.sh');

Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: executing shell script from php

Mon Feb 04, 2013 10:26 pm

It could need the full path to the "exec" command. Or run a batch file which calls this, which contains some debug. I'd give you en example but I am at work and cane "see" my rPis.
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display

Return to “Troubleshooting”