WildCat
V2EX  ›  Node.js

求个 child_process 自动重启(crash 不会引起主进程退出,并自动重启)的代码示例

  •  
  •   WildCat · May 26, 2014 · 3598 views
    This topic created in 4433 days ago, the information mentioned may be changed or developed.
    7 replies    2014-06-19 09:42:00 +08:00
    heiher
        1
    heiher  
       May 27, 2014 via iPhone
    #!/bin/bash

    while true; do
    # run your app in foreground
    done
    WildCat
        2
    WildCat  
    OP
       May 27, 2014 via iPhone
    @heiher 抱歉,BAE环境,只能纯node实现
    rekey
        3
    rekey  
       May 27, 2014
    代码运行在
    try{}catch(e){}
    里试试?
    withinthefog
        4
    withinthefog  
       May 27, 2014
    WildCat
        5
    WildCat  
    OP
       May 27, 2014
    @withinthefog BAE环境 似乎不能用这个。
    withinthefog
        6
    withinthefog  
       May 28, 2014   ❤️ 1
    cluster.on('exit', function(worker, code, signal) {
    console.log('worker %d died (%s). restarting...',
    worker.process.pid, signal || code);
    cluster.fork();
    });

    要纯手写的话大概就是这个样子了,不知道你的child_process是不是用cluster模块手工创建的
    可以参考node.js官方的cluster模块文档:
    http://nodejs.org/api/cluster.html#cluster_cluster
    Honwhy
        7
    Honwhy  
       Jun 19, 2014   ❤️ 1
    楼主你需要的是这个么?
    ```
    var cp = require('child_process');

    var worker;

    function spawn(server, config) {
    worker = cp.spawn('node', [ server, config ]);
    worker.on('exit', function (code) {
    if (code !== 0) {
    spawn(server, config);
    }
    });
    }

    function main(argv) {
    spawn('server.js', argv[0]);
    process.on('SIGTERM', function () {
    worker.kill();
    process.exit(0);
    });
    }

    main(process.argv.slice(2));
    ```
    [七天学会NodeJS](http://nqdeng.github.io/7-days-nodejs/)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1288 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 17:16 · PVG 01:16 · LAX 10:16 · JFK 13:16
    ♥ Do have faith in what you're doing.