【发布时间】:2014-09-01 03:29:47
【问题描述】:
我正在尝试通过 php 脚本在本地项目中执行 grunt。
我已经全局安装了 npm 和 grunt cli。
如果我打开终端并输入:
cd /path/to/local/grunt/project/ && grunt
Grunt 将成功运行并执行我在该目录中的 gruntfile.js 中设置的任务。
但是,当我尝试通过 php 脚本执行同样的事情时
var_dump(shell_exec('cd /path/to/local/grunt/project/ && grunt 2>&1'));
我明白了:
sh: grunt: command not found
我也尝试了全局 CLI 的直接路径:
var_dump(shell_exec('/usr/local/lib/node_modules/grunt-cli/bin/grunt 2>&1'));
然后我得到:
env: node: No such file or directory
但是,这会从终端按预期运行。
这里发生了什么?为什么当我尝试通过 php 运行时找不到 grunt 命令?如何从 php 脚本中执行 shell exec 或 exec grunt?
【问题讨论】:
-
使用命令行时的PATH变量和加载的不一样
-
我可以运行其他类似“/usr/local/bin/optipng”的文件,这些文件和 grunt 在同一个地方。有什么建议吗?
-
我认为您有 PATH 问题。在这里试试这个帖子:serverfault.com/questions/151328/…