【问题标题】:How can I spawn a new process and quit from a node.js script?如何生成一个新进程并退出 node.js 脚本?
【发布时间】:2014-02-23 09:42:17
【问题描述】:

我有一个 nodeJS 命令行脚本,它通过 casperJS/spookyJS 使用 phantomJS 与网站交互。重定向事件正在阻止我的进程正常工作。我将如何响应重定向,生成一个新进程并终止已重定向的进程?

例如:

spooky.on('navigation.requested', function(url, navigationType, navigationLocked, isMainFrame){
    if (url.toString().match(/(http:\/\/url(.|\n)to(.|\n)match\/client\/authentication\/login)/i)) {
        utils.log('redirect happened');
        //get my own process id, launch a new process, get that id.
        //kill this id?
    }
});

我会怎么做:

  • 获取当前运行的节点进程的进程ID(第一个 运行上述代码 - 进程 A)?
  • 生成一个新的节点进程(进程 B),该进程将独立于 进程A
  • 杀死进程A

即使进程 A 终止,进程 B 也应该继续运行。我是否需要第三个节点进程 (C) 来管理进程 A 和 B?

【问题讨论】:

    标签: node.js phantomjs casperjs spookyjs


    【解决方案1】:

    child_process.spawn() 中使用'detached' option 您可能还想从进程中分离tty(使用nohup 实用程序)

    【讨论】:

    • 谢谢。如果我杀死父进程,子进程会继续运行吗?
    • 是的:If the detached option is set, the child process will be made the leader of a new process group. This makes it possible for the child to continue running after the parent exits.
    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2018-07-08
    • 2016-09-25
    相关资源
    最近更新 更多