简体   繁体   中英

Command not found: PHP exec()

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?

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>'); eg 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.

弄清楚了,我还需要定义节点:

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

Create a symbolic link for forever

ln -s /usr/local/bin/forever /usr/bin/env/forever

And also for nodejs if incase it's still called "nodejs". Make it call as "node"

ln -s /usr/bin/nodejs /usr/bin/node

I will solve the forever execution problem.

For php side, try with this

echo shell_exec("your command sh");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM