简体   繁体   中英

Execute a PM2 Command With Node.js

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! microscope_application@0.0.1 start: `node ./bin/www` npm ERR! Exit status 8 npm ERR! npm ERR! Failed at the microscope_application@0.0.1 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('/'); }); 

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

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