【问题标题】:Not able to create child Process in node.js无法在 node.js 中创建子进程
【发布时间】:2021-08-04 13:44:30
【问题描述】:

我正在使用 node.js 进行日志尾项目,但函数 process.createChildProcess 无法正常工作。是否有任何替代方法?

var sys = require('sys');
var filename = "test.txt";
var process=require('process')

if (!filename)
  return sys.puts("Usage: node watcher.js filename");

// Look at http://nodejs.org/api.html#_child_processes for detail.

var tail = process.createChildProcess("tail", ["-f", filename]);
sys.puts("start tailing");

tail.addListener("output", function (data) {
  sys.puts(data);
});

// From nodejs.org/jsconf.pdf slide 56
var http = require("http");
http.createServer(function(req,res){
  res.sendHeader(200,{"Content-Type": "text/plain"});
  tail.addListener("output", function (data) {
    res.sendBody(data);
  });
}).listen(8000);

这里出现错误:

var tail = process.createChildProcess("tail", ["-f", filename]);
                   ^

TypeError: process.createChildProcess is not a function

任何帮助将不胜感激

文章引用:https://thoughtbot.com/blog/real-time-online-activity-monitor-example-with-node-js-and-websocket

【问题讨论】:

    标签: javascript node.js process


    【解决方案1】:

    您可以使用spawn 在 Node.JS 中创建子进程。 这是基于您的代码的示例:

    const { spawn } = require('child_process');
    /* Some Code Here! */
    var tail = spawn("tail", ["-f", filename]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      相关资源
      最近更新 更多