php script to run shell command

pkromer
pkromer used Ask the Experts™
on
I have the following in a php file:

<?php
$output = shell_exec(dirname(__FILE__) . '/add_sku_to_get_flat_rate_quotes.sh');
?>

When I call that file from a browser, it is supposed to run this .sh command in another file in same dir:

me@myserver:~/public_html/mywebsite.com/symfony$ app/console myaccount:get_flat_rate_quotes sku 7017

Which is supposed to add that sku to a table. When I run that sh command from an SSH terminal, it works. What am I missing?

My web host confirmed shell_exec will work on my server.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
It's likely an environmental or permissions issue. When YOU run that command from a shell, you have logged into a shell (probably bash) with YOUR account.

Depending on how your web host is set up, PHP is probably running from either a general web server user like "www" or "nobody" or it's a setup that does a setuid so the web server child process runs as your account.

Either way, the web server user probably doesn't have the same environmental variables / settings. If your shell script depends on any of them, then it will fail if they're not set.

If the web server runs as a generic user account, then maybe the shell script only has permissions to allow YOUR account to run it (or maybe it calls some other command that isn't accessible to a generic user).
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
suEXEC is normally required for a web / PHP user to run scripts with the owner's permissions instead of the generic web server permissions.  https://en.wikipedia.org/wiki/SuEXEC  Not all web hosts will enable it.
Fractional CTO
Distinguished Expert 2018
Commented:
1) Start by running a phpinfo.php file with the contents...

<?php
print_r (phpinfo());
?>

Open in new window


This will verify PHP is working.

2) Also, use an absolute filename. You have a relative path which will work or fail, depending on the CWD (current working directory) at time of run... which is anyone's guess.

Likely this is the problem.
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

David FavorFractional CTO
Distinguished Expert 2018

Commented:
Also, be sure to print $output + add a comment with whatever output was returned.

Author

Commented:
php is working, I checked using the above snippet.

Setting the sh file to 777 didnt help, changing everything to absolute path didnt help, nothing helped. Are my two files above correct? Should the sh command look like it does? Do I have to prefix it with sh?

Author

Commented:
finally got it... was just a tweak to my .sh file...

php file:

<?php
$output = shell_exec(dirname(__FILE__) . '/add_sku_to_get_flat_rate_quotes.sh');
?>

.sh file:

/usr/home/me/public_html/mysite.com/symfony/app/console myaccount:get_flat_rate_quotes sku 7017

Author

Commented:
as David said, absolute path. I set it wrong before.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial