2

I'd like to execute pm2 restart IDOFMYAPP using NodeJS... Is that possible?

Something like that :

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }
exec("pm2 restart IDOFMYAPP", puts);

Actually, I want to create a button to execute this code above but my button is in a view in my app that I want to restart...

EDIT #1

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1042:14)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Function.app.listen (C:\xampp\htdocs\CMS\node_modules\microscope-web\node_modules\express\lib\application.js:546:24)
    at _.extend.run (C:\xampp\htdocs\CMS\node_modules\microscope-web\libs\Application.js:109:27)
    at Application.extend.initialize (C:\xampp\htdocs\CMS\Application.js:23:10)
    at Application (C:\xampp\htdocs\CMS\node_modules\microscope-web\libs\Application.js:31:19)
    at new child (C:\xampp\htdocs\CMS\node_modules\microscope-web\libs\utils.js:17:27)
    at C:\xampp\htdocs\CMS\bin\www:11:16

npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is most likely a problem with the microscope_application package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls microscope_application
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.2.9200
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! cwd C:\xampp\htdocs\CMS
npm ERR! node -v v0.10.33
npm ERR! npm -v 1.4.28
npm ERR! code ELIFECYCLE
npm

EDIT #2

var shell = require('shelljs');

shell.exec('pm2 restart IDOFMYAPP', function(code, output) {
  console.log('Exit code:', code);
  console.log('Program output:', output);
  res.redirect('/');
});

1 Answer 1

3

You can do it like so:

var shell = require('shelljs');

shell.exec('pm2 restart IDOFMYAPP', function(code, output) {
  console.log('Exit code:', code);
  console.log('Program output:', output);
});
Sign up to request clarification or add additional context in comments.

5 Comments

EADDRINUSE means that you have another service running on the same port.
Ok, so it works now, but how to redirect to '/' after success exec??
Can you elaborate on that? It sounds like creating another question might be more appropriate.
See edit#2 above... Because your code works but my web page seems to not appreciate the pm2 restart IDOFMYAPP...

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.