运行没有需要root权限的密码的linux命令

时间:2017-09-12 08:46:32

标签: node.js

我使用node在Linux上执行bash命令。我需要停止并启动Apache服务器。通常来自bash,你需要输入:

sudo service apache2 stop

您需要输入密码。为了改变这一点,我修改了' visudo'修改后没有要求输入密码但是当我从节点运行时我得到错误

Error: Command failed: /bin/sh -c sudo systemctl status apache2.service
sudo: no tty present and no askpass program specified

问题出在哪里?节点代码看起来像

app.get('/StatusTest', function(req, res){
    exec("sudo systemctl status apache2.service", function(err, stdout){
        if(err) throw err;
        console.log(stdout);
        res.render('statustest');
   });
})

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此命令进行自动密码输入:

exec(`echo 'your_sudo_password' | sudo -S systemctl status apache2.service`, ...);
相关问题