views:

259

answers:

3

Hey,

I want to use php's exec() function on an ubuntu server. The problem is, I alway get an error, that the command is not found. For example using

exec("echo 123");

prints

sh: /echo: not found

To me, it looks like php is using the sh shell, when I want to be using bash. I tried changing the shell for www-data in /etc/passwd, that didn't help either.

Does anybody have an idea where else the problem might be coming from or how I can change the shell for php's ubuntu user.

Thanks, Timo


[EDIT]

Maybe this helps:

I call a bash script from ssh as timo, this script calls a php script, which uses exec. I know, it sounds weird, but it's part of a bigger development environment...

The point is, I'm not ever certain, as which user the script inside exec is executed.


[EDIT]

By now I figured out that there must be another rights problem involved. Even if I try calling a bash script test.sh (by it's full path!) from within exec, php test.php will just say.

sh: /test.sh: not found

A: 

I think the problem is that there is no $PATH setup. Try using full paths to your binaries ie /bin/echo

camomileCase
Thanks for you super quick reply.Unfortunately, that didn't solve the problem. /bin/echo doesn't work either.What I actually wanted to do is this/usr/bin/mysql --user=asdf --password=asdf mydb < ./dump.sqlAs you can see, I even used the absolute path. The echo was just an example to make it more simple...Any other ideas maybe?
Timo
A: 

If what you want to do is:

/usr/bin/mysql --user=asdf --password=asdf mydb < ./dump.sql

Then I imagine this would work (regardless of shell):

/usr/bin/mysql --user=asdf --password=asdf mydb < /full/path/to/dir/dump.sql

Iain Collins
Oh, and I'm not sure how to actually set the shell in PHP, but you could call a script from sh with "#!/bin/bash" as it's first line, if you really do need bash (and can't find another way to do it).
Iain Collins
the problem is not, that my dump cannot be found, but rather mysql can't be found...
Timo
If there really is a binary installed at /usr/bin/mysql then sh would find it and the problem is something else - like maybe safe-mode is enabled (checkout the docs for safe-mode-exec-dir and similar). Another thing to check is open_basedir, which can also limit what exec has access too.
Iain Collins
A: 

Try shell_exec() instead. exec should not invoke ANY shell to execute your program. Alternately, you can invoke bash with exec like

exec("/bin/bash -c \"echo $foo > bar.txt'\"")

Trey
this will just say "sh: /bash: not found". looks like php searches for all the binaries in /. in php.ini safe_mode_exec_dir is set to nothing, so i guess that means, there are no restrictions!? (changing it doesn't seem to help)
Timo
I have no experience with safe mode so perhaps that is your problem. Try turning it off? You shouldn't be using it anyway, it's going away and was a bad idea from the beginning (list most PHP language "features") http://php.net/manual/en/features.safe-mode.php
Trey
Didn't notice someone had mentioned this already - agreed safe mode is Considered Harmful. Regarding safe_mode_exec_dir, if safe mode is on and this is set to empty then that would explain it (if you want to run anything it would need to set to /). I suggest you try setting it to something meaningful and invoking something in that path.
Iain Collins