4

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?

5
  • Yeah, /user/... should be a typo of /usr/...? Commented Nov 14, 2012 at 11:25
  • Yeah, I just fixed that. Now the error is /usr/bin/env: node: No such file or directory Commented Nov 14, 2012 at 11:28
  • The exec function is a dangerous function, should not be used and be disabled in your php.ini. Using exec to solve problems of your web application is not the right way. Commented Nov 14, 2012 at 11:41
  • If I could think of another way to restart a node script via the web, I'd do it. But nothing comes to mind. Commented Nov 14, 2012 at 11:44
  • I might need to add that what I'm saying applies for web applications, not for console apps of course. Commented May 9, 2013 at 9:13

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Added that line, and then removed the /usr/local/bin prefix to forever, and I reverted back to the forever not found error.
Same for echo exec('export PATH=$PATH:/usr/local/bin; sudo -u dalton forever restart botti.js 2>&1');
have you tried sudo -E? sudo may be configured to reset the PATH, in that case you would have to change the PATH inside your forever script EDIT: ah, I see you figured it out. Anyway, still seems like PATH is wrong
I know, and I'm still trying to figure it out, but this sloppy bit of code works for now. Thanks for helping!
2

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

Comments

0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.