WildCat
V2EX  ›  Node.js

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

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

    while true; do
    # run your app in foreground
    done
    WildCat
    WildCat
    OP
    May 27, 2014 via iPhone
    2
    @heiher 抱歉,BAE环境,只能纯node实现
    rekey
    rekey
    May 27, 2014
    3
    代码运行在
    try{}catch(e){}
    里试试?
    WildCat
    WildCat
    OP
    May 27, 2014
    5
    @withinthefog BAE环境 似乎不能用这个。
    withinthefog
    withinthefog
    May 28, 2014❤️1
    6
    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
    Honwhy
    Jun 19, 2014❤️1
    7
    楼主你需要的是这个么?
    ```
    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   ·   Privacy   ·   Solana   ·   2069 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:51 · PVG 20:51 · LAX 04:51 · JFK 07:51
    ♥ Do have faith in what you're doing.