user3143218
user3143218

Reputation: 1816

How to run grunt from a PHP script?

I'm trying to execute grunt in a local project from a php script.

I have npm and the grunt cli installed globally.

If I open up terminal and enter:

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

Grunt will run successfully and carry out the tasks I've set in the gruntfile.js found in that directory.

However, when I try to shell exec the same thing via a php script

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

I get:

sh: grunt: command not found

I've also tried the direct path to the global CLI:

var_dump(shell_exec('/usr/local/lib/node_modules/grunt-cli/bin/grunt 2>&1'));

but then I get:

env: node: No such file or directory

However this runs as expected from terminal.

What's going on here? Why is the grunt command not found when I try to run it via php? How can I shell exec or exec grunt from a php script?

Upvotes: 3

Views: 2350

Answers (1)

Khanakia
Khanakia

Reputation: 742

You need to use absolute paths to node and grunt so execute your command like this

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

Upvotes: 4

Related Questions