Arnas Pecelis
Arnas Pecelis

Reputation: 35

shell_exec not working when calling node

Im trying to execute the node command from PHP with this function:

$output = shell_exec('node -v');
echo $output; // null

and it returns me null instead of version. Why it's not working? Node.js is installed on the server and working when executing from shell.

Upvotes: 0

Views: 694

Answers (1)

Mark Davidson
Mark Davidson

Reputation: 5513

The documentation for the shell_exec function says

This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.

Its possible that node -v dosn't output to stdout or that another error is occurring. Could you maybe try another program such as uptime, its also worth noting it won't work if your PHP is running in safe_mode.

Upvotes: 1

Related Questions