Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command not found: PHP exec()

Tags:

shell

php

node.js

This is driving me crazy. I need to have php execute a command to restart a script running in node. I'm using a node app called forever to run said script. 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

Next, I try which forever and type forever, both which give me:
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?

like image 667
Dalton Gore Avatar asked Jan 16 '23 01:01

Dalton Gore


2 Answers

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)

I suspect forever calls /usr/bin/env node. The error from env is probably caused by node being outside your PATH too.

To set your PATH in php, use putenv('PATH=<your path here>'); e.g. to append /usr/local/bin:

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

This may also be a sudo issue, try the -E (preserve environment) switch.

like image 174
crater2150 Avatar answered Jan 17 '23 16:01

crater2150


Figured it out, I needed to define node as well:

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

like image 43
Dalton Gore Avatar answered Jan 17 '23 15:01

Dalton Gore