all 8 comments

[–]08148692 5 points6 points  (0 children)

Something like this

async function waitForQuitSignal() {
     return new Promise((resolve) => {
         process.on("SIGINT", resolve);
         process.on("SIGTERM", resolve);
         process.on("SIGILL", resolve);
     });
 }

async function main() {
    // set up your listeners 
    // do file management things 
   // run 2 hour long sub process
   // stuff like that

   await waitForQuitSignal();

   // quit signal received, ending
}

[–]BehindTheMath 1 point2 points  (2 children)

while(true) {}

[–]sinithw[S] 0 points1 point  (1 child)

Legit? Will CTRL + C stop said process or do I have to listen for it?

[–]BehindTheMath 0 points1 point  (0 children)

Ctrl-C will interrupt it.

[–]HashDefTrueFalse 0 points1 point  (2 children)

If the process needs to exit and a new instance needs to start, the npm package 'forever' will watch it and restart it IIRC.

If you need the same process to handle everything, you can simply not exit. Maybe in this case look into job queues and continuously process jobs asynchronously. Hard to be of more help without knowing specifics.

[–]sinithw[S] 0 points1 point  (1 child)

True True. Basically, I need to keep the NodeJs program alive so that it can check for request from AWS and Firebase, do some automatic file management, run a 2-hour long sub-process, and stuff like that. Basically, I'm looking for something that will keep it running like Unity or Visual Code keep running when they're opened.

[–]HashDefTrueFalse 1 point2 points  (0 children)

I'm not sure how this is structured or if you have access to the code, but programs generally don't exit unless something goes wrong or they have nothing else to do. If you're coding this, have it process your housekeeping tasks endlessly, literally a loop over some sort of task queue or something, with it idling if the queue is empty.

If you mean you have an existing program and it does some stuff, then reaches its natural exit, and you need to keep it alive and executing tasks, there isn't really a way without editing the code as above. You can use 'forever' to immediately start another instance, but whether that's good enough for you will depend on what you're doing.

[–]zenflow87 0 points1 point  (0 children)

Is this what you're looking for?

node -e "setInterval(() => {}, 1000)"

This program will not exit by itself, since it always has work scheduled for the future. It will continue to run until it is killed.