简体   繁体   English

找不到命令:P​​HP exec()

[英]Command not found: PHP exec()

This is driving me crazy. 这真让我抓狂。 I need to have php execute a command to restart a script running in node. 我需要php执行一个命令来重启节点中运行的脚本。 I'm using a node app called forever to run said script. 我正在使用一个名为forever的节点应用来运行所述脚本。 The code is as follows: 代码如下:

<?php 
  echo '<pre>';
  echo exec('sudo -u dalton forever restart botti.js 2>&1');
  echo '</pre>';
?>

However, when I run that, I get sudo: forever: command not found 但是,当我运行它时,我得到sudo: forever: command not found

Next, I try which forever and type forever , both which give me: 接下来,我尝试which foreverwhich forever type forever ,这两个给我:
forever: /usr/local/bin/forever

I edit my code to: 我编辑我的代码:
echo exec('sudo -u dalton /usr/local/bin/forever restart botti.js 2>&1');

Edit: After a typo, the error is now: 编辑:输入错误后,错误现在是:
/usr/bin/env: node: No such file or directory

I'm at my wit's end. 我的智慧结束了。 Any ideas? 有任何想法吗?

As the forever command only runs, when you give the full path, I suspect, that /usr/local/bin is not in your PATH environment variable, which contains all directories, that are searched for executable commands by default, separated by : (I suspect you're on Linux, may differ for other OS) 由于forever命令只运行,当你给出完整路径时,我怀疑, /usr/local/bin不在你的PATH环境变量中,它包含所有目录,默认情况下搜索可执行命令,用:分隔:我怀疑你在Linux上,可能与其他操作系统有所不同)

I suspect forever calls /usr/bin/env node . 我怀疑forever调用/usr/bin/env node The error from env is probably caused by node being outside your PATH too. env的错误可能是由nodePATH之外引起的。

To set your PATH in php, use putenv('PATH=<your path here>'); 要在php中设置PATH,请使用putenv('PATH=<your path here>'); eg to append /usr/local/bin : 例如,添加/usr/local/bin

putenv('PATH=' . getenv('PATH') . ':/usr/local/bin')

This may also be a sudo issue, try the -E (preserve environment) switch. 这也可能是一个sudo问题,尝试-E (保护环境)切换。

弄清楚了,我还需要定义节点:

$asdf = system('sudo -E -u dalton /usr/local/bin/node /usr/local/bin/forever restart botti.js 2>&1');

Create a symbolic link for forever 永远创建一个符号链接

ln -s /usr/local/bin/forever /usr/bin/env/forever

And also for nodejs if incase it's still called "nodejs". 而对于nodejs,如果它仍然被称为“nodejs”。 Make it call as "node" 将其称为“节点”

ln -s /usr/bin/nodejs /usr/bin/node

I will solve the forever execution problem. 我将解决永远执行问题。

For php side, try with this 对于php端,请尝试使用此功能

echo shell_exec("your command sh");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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