Reputation: 693
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
Upvotes: 14
Views: 11847
Reputation: 9156
I had a problem with Debian Docker PHP image, this helped me:
https://www.reliableembeddedsystems.com/wiki/index.php?title=Make_bash_default_shell
/bin/sh
was a symlink to dash, which may be better but doesnt work with my scripts.
echo "dash dash/sh boolean false" | debconf-set-selections
dpkg-reconfigure -f noninteractive dash
Upvotes: 0
Reputation: 636
I think PHP calls /bin/sh internally (or cmd.exe on Windows) regardless of which function you use.
Running ls -l /proc/$$/exe | sed 's%.*/%%'
in your terminal in a Linux system should tell you what shell you're using, which is bash in my Ubuntu terminal by default. If you run the following in Ubuntu:
<?php
exec('ls -l /proc/$$/exe | sed \'s%.*/%%\'', $res);
// or:
//$res = shell_exec('ls -l /proc/$$/exe | sed \'s%.*/%%\'');
echo(json_encode($res));
you will get ["dash"] as the result, regardless of which function you use. If you change /bin/sh to point to bash rather than dash, the result from PHP changes to bash, but I don't recommend doing that obviously.
So long story short it's probably not possible to change this without modifying your entire server's default shell or compiling PHP from source.
If you want to solve the mystery further, it's probably related to ext/standard/proc_open.c and ext/standard/exec.c (all these functions are in there, system, passthru, etc.) in the PHP source, but to be honest it beats me.
Upvotes: 0
Reputation: 11158
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'\"")
Upvotes: 15
Reputation: 6884
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
Upvotes: 0
Reputation: 1706
I think the problem is that there is no $PATH setup. Try using full paths to your binaries ie /bin/echo
Upvotes: 0