2014-09-01 132 views
3

我想从php脚本执行本地项目中的grunt。如何从PHP脚本运行grunt?

我有npm和全球安装的grunt cli。

如果我打开终端,输入:

cd /path/to/local/grunt/project/ && grunt 

步兵成功运行,开展我在该目录中的gruntfile.js设定的任务。

然而,当我尝试通过PHP脚本壳EXEC同样的事情

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脚本中调用exec或exec grunt?

+0

PATH变量是不是如由 – 2014-09-01 03:47:00

+0

加载,我可以运行其他人一样一样“/ usr/local/bin/optipng”在grunt的相同位置找到。有什么建议么? – user3143218 2014-09-01 04:01:05

+0

我认为你有PATH问题。试试这篇文章:http://serverfault.com/questions/151328/setting-apache2-path-environment-variable – 2014-09-01 04:14:15

回答

4

您需要使用节点和繁重的绝对路径,以便使用命令行的时候执行你的命令,这样

var_dump(shell_exec('cd /path/to/local/grunt/project/ && /usr/local/bin/node /usr/local/bin/grunt 2>&1'));