prevent NodeJS program from exiting

后端 未结 2 569
我寻月下人不归
我寻月下人不归 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.

    0 讨论(0)
  • 2021-02-08 19:31

    I think the best way to prevent exit from process is:

    process.stdin.resume();
    

    I found this solution here https://stackoverflow.com/a/14032965/2094090 and it works fine.

    0 讨论(0)
提交回复
热议问题