Running a shell command from Node.js without buffering output

前端 未结 4 684
傲寒
傲寒 2021-01-30 10:37

I\'m trying to launch a shell command from Node.js, without redirecting that command\'s input and output -- just like shelling out to a command using a shell script, or

4条回答
  •  有刺的猬
    2021-01-30 10:53

    There's an example in the node docs for the child_process module:

    Example of detaching a long-running process and redirecting its output to a file:

     var fs = require('fs'),
         spawn = require('child_process').spawn,
         out = fs.openSync('./out.log', 'a'),
         err = fs.openSync('./out.log', 'a');
    
     var child = spawn('prg', [], {
       detached: true,
       stdio: [ 'ignore', out, err ]
     });
    
     child.unref();
    

提交回复
热议问题