Home  >  Article  >  Backend Development  >  What should I do if exec php cannot be executed?

What should I do if exec php cannot be executed?

藏色散人
藏色散人Original
2021-03-01 09:34:082457browse

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.

What should I do if exec php cannot be executed?

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

Why can't php exec be executed?

Origin

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);

Problem location
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"]

Solution Solution
  • Solution 1: Modify the first line of the yii file to directly specify the php interpreter;
    is changed from #!/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);
  • Option 2: Modify the exec calling method and directly specify the php interpreter.
exec("/usr/local/bin/php /home/app/example/api/app/yii demand/virtual {$id} 2>&1", $output);
print_r($output);
  • Note: In actual execution, there are still some problems with the exec command, see "504 Error" below;
504 Error
  • When the exec time is too long (such as more than 60 seconds), a 504 error (Gateway Timeout) will appear on the page;
  • Need to redirect the standard output and standard error, and then the background process will Execute the command, and the processing process number will be output on the page;
$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);
About /usr/bin/env (high portability?)
  • Why is it better to use “#!/usr/bin/env NAME” instead of “#!/path/to/NAME” as my shebang?
  • How does /usr/bin/env know which program to use?
nginx user

For security reasons, the nginx user's shell is usually set to /sbin/nologin

What is the relationship between scheduled tasks and Web operations?

The relationship between scheduled tasks and click operations on the OSS background management page has not been handled well;

  1. Two processing methods of the yii framework console and web;
    The issue of how to improve the testability of crontab;
    The issue of how to improve the crontab user experience;
    The issue of how to reasonably reuse crontab tasks and API (Controller/Action) code;
  2. Foreground and backend The way to share Controller/Action;
    Or the problem of conveniently sharing code libraries between multiple projects;
Enlightenment
  • php-fpm does not work exec, system, shell_exec, only CLI;
  • PHP exec() does not run all commands;

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn