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.
LinuxShell ScriptingPHP

Avatar of undefined
Last Comment
pkromer

8/22/2022 - Mon
gr8gonzo

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 Baldwin

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
David Favor

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
David Favor

Also, be sure to print $output + add a comment with whatever output was returned.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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?
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
pkromer

ASKER
as David said, absolute path. I set it wrong before.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.