Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the shell for php's exec()

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

like image 452
Timo Avatar asked Nov 24 '09 20:11

Timo


1 Answers

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'\"")
like image 58
Trey Avatar answered Sep 20 '22 11:09

Trey