简体   繁体   中英

shell_exec(); not working properly in PHP

I'm trying to execute a command thru shell_exec(); but it does nothing. I know shell_exec(); is enabled because these work perfectly:

echo shell_exec('which php');
echo shell_exec('ls');

They do output correctly.

The problem is when I want to execute something like this:

shell_exec("php file.php var1 var2 > /dev/null &");

It does nothing, but when I run it thru the Terminal on my computer, it does work. I have also tried doing it with the full path like this:

shell_exec("/usr/bin/php /full/path/file.php var1 var2 > /dev/null &");

And it doesn't work, but again... it works if I run it thru my Terminal's computer.

The php.ini safe mode is off, and there are no disabled functions on it, so I don't know what could be happening. And yes, I have access to SSH and got a dedicated IP.

Try to look at return code $return_var of the function (last arguments of functions system exec ). It should be 0 if success. If it is 127 possibly command line is not permitted on the server or you don't have enough privilege to run commands from PHP scripts (it depends on the user/group from which scripts are executed). Otherwise function simply returns empty array of output, like nothing good or bad happened.

I ran into similar situation not so long ago, ls was working Ok, while wget was always failing.

You are probably get a permission denied error especially since you are trying to redirect the output to /dev/null . Might want to consider a cron job but I'm not sure why you are redirecting the output and running it in the background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM