Link to home
Start Free TrialLog in
Avatar of pkromer
pkromer

asked on

php script to run shell command

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.
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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).
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.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, be sure to print $output + add a comment with whatever output was returned.
Avatar of pkromer
pkromer

ASKER

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?
Avatar of pkromer

ASKER

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
Avatar of pkromer

ASKER

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