Home >Backend Development >PHP Problem >What should I do if exec php cannot be executed?
Solution to the problem that exec php cannot be executed: 1. Modify the first line of the yii file to directly specify the php interpreter; 2. Modify the exec calling method to directly specify the php interpreter.
#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
Why can't php exec be executed?
There is a scheduled task. I want to click to start it on the OSS background management page, but I have tried several methods but it doesn’t work; no matter system or exec (manually executing it on the shell command line, there is no problem) ); Initially I suspected a permission issue with the Web Server nginx user, but later found that the environment variable could not find the php executable file (because the nginx user is a restricted user);
exec("/home/app/example/api/app/yii demand/virtual {$id}", $output); print_r($output);
and above After the script is executed, only Array()
is output. Obviously, the business code is not executed (the first line of the business code is output);
// http://oss.example.com/index.php?r=demand/demand-ip/msg&id=1929 点击后执行 exec("/home/app/example/api/app/yii demand/virtual {$id} 2>&1", $output); print_r($output);
There is an error output after the above script is executed: Array ([0] => /usr/bin/env: php: No such file or directory )
;
/usr/bin/env: php
This The sentence comes from the first line of the yii file #!/usr/bin/env php
, which is intended to indicate where the php script interpreter that executes the yii file is; but the error output is /usr/bin /env: php: No such file or directory
It means that php cannot be found;
[Recommended learning: "PHP Video Tutorial"]
#!/usr/bin/env php
to # !/usr/local/bin/php
, that is, directly specify the location of the php interpreter (you can also modify it when the code is released); exec("/usr/local/bin/php /home/app/example/api/app/yii demand/virtual {$id} 2>&1", $output); print_r($output);
$cmd = '/usr/local/bin/php /home/app/example/yapp/yii demand/finish 2057'; $pid_file = 'msgrepeat.pid'; $exec_cmd = sprintf("%s > /dev/null 2>&1 & echo $! > %s & cat %s", $cmd, $pid_file, $pid_file); exec($exec_cmd);
For security reasons, the nginx user's shell is usually set to /sbin/nologin
The relationship between scheduled tasks and click operations on the OSS background management page has not been handled well;
The above is the detailed content of What should I do if exec php cannot be executed?. For more information, please follow other related articles on the PHP Chinese website!