node.js执行shell命令进行服务器重启

nodejs功能强大且多样,不只是可以实现 服务器端 与 客户端 的实时通讯,另一个功能是用来执行shell命令

1、首先,引入子进程模块

var process = require('child_process');

2、然后,调用该模块暴露出来的方法exec

process.exec('shutdown -h now',function (error, stdout, stderr) {
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});

回调函数非必须!

3、具体案例 

var express = require('express');
var app = express();
var process = require('child_process');

app.get('/restart/ccss', function (req, res) {
    res.send('OK');
    process.exec('shutdown -r now',function (error, stdout, stderr) {
        if (error !== null) {
            console.log('exec error: ' + error);
        }
    });
})

var server = app.listen(8081, function () {
    var host = server.address().address
    var port = server.address().port
})

 4、Linux下解决“shutdown: command not found"问题

1、首先查看shutdown命令的所在位置,即路径,输入语句whereis shutdown,找到其位置。

2、然后查看环境变量配置文件.bash_profile,在文件的最后加入PATH=$PATH:xxxx。(如果shutdown在/sbin/shutdown,那么xxxx就是/sbin)

3、之后用source ~/.bash_profile执行一下文件。这样就可以正常使用shutdown了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值