prevent NodeJS program from exiting

后端 未结 2 568
我寻月下人不归
我寻月下人不归 2021-02-08 19:09

I am creating NodeJS based crawler, which is working with node-cron package and I need to prevent entry script from exiting since application should run forever as

2条回答
  •  清酒与你
    2021-02-08 19:29

    Because nodejs is single thread a while(true) will not work. It will just grab the whole CPU and nothing else can ever run.

    nodejs will stay running when anything is alive that could run in the future. This includes open TCP sockets, listening servers, timers, etc...

    To answer more specifically, we need to see your code and see how it is using node-cron, but you could keep your nodejs process running by just adding a simple setInterval() such as this:

    setInterval(function() {
        console.log("timer that keeps nodejs processing running");
    }, 1000 * 60 * 60);
    

    But, node-cron itself uses timers so it appears that if you are using node-cron properly and you correctly have tasks scheduled to run in the future, then your nodejs process should not stop. So, I suspect that your real problem is that you aren't correctly scheduling a task for the future with node-cron. We could help you with that issue only if you show us your actual code that uses node-cron.

提交回复
热议问题