node.js server to execute shell commands to restart

nodejs powerful and diverse, not just instant messaging server and client can be achieved, and the other function is used to execute shell commands

1, first, the introduction of sub-process module
var = the require Process ( 'child_process');
2, then the calling method exec module exposed
process.exec ( 'shutdown -h now', function (error, stdout, stderr) {
IF (error == null!) {
the console.log ( 'Exec error:' + error);
}
});
callback not necessarily!

3, the specific case 
var = Express the require ( 'Express');
var App = Express ();
var = the require Process ( 'child_process');

app.get ( '/ the restart / CCSS', function (REQ, RES) {
RES .send ( 'the OK');
process.exec ( 'the shutdown -R & lt now', function (error, stdout, stderr) {
IF (error == null) {!
the console.log ( 'Exec error:' + error);
}
});
})

var = app.listen Server (8081, function () {
. = server.address Host var () address
. var = server.address Port () Port
})
 . 4, Linux, solution "shutdown: command not found "problem
1, first check the location of the shutdown command, that is the path, the input statement whereis shutdown, find its location.

2, and then view the environment variable configuration file .bash_profile, the last to join in the file PATH = $ PATH: xxxx. (If the shutdown in / sbin / shutdown, then xxxx is the / sbin)

3, followed by source ~ / .bash_profile executed at the file. In this way you can normally use the shutdown.

Guess you like

Origin www.cnblogs.com/zzsdream/p/11972609.html