nodejs child_processes.exec无法返回'nohup'命令

hyp*_*ion 0 javascript shell node.js

我编写了一个nodejs函数来执行'nohup'命令,并将成功结果作为http响应发送.

function _nohup(cmd,res){
    var child = exec('nohup ./' + cmd + '.sh &',
    function (error, stdout, stderr) {
        res.writeHeader(200);
        res.end("start process success!");
    });
}
Run Code Online (Sandbox Code Playgroud)

但是当我通过url地址调用函数时,响应数据无法返回.

Mat*_*ney 6

child_process.exec()等待子进程退出然后调用回调.在你的情况下,你已经产生了一个后台进程,大概是没有退出.

你可能想要child_process.spawn():

http://nodejs.org/docs/v0.4.9/api/child_processes.html#child_process.spawn