how to set a node-schedule job that it doesn't destroy after server reboot in node js

Viewed 781

I made a scheduled job by the following code:

const schedule = require('node-schedule');

scheduleAuto1A = schedule.scheduleJob(time, function(){
    // do some
});

a user enter the cron job time and it have to work until the user cancel it. but the problem is:
When i restart my app or reboot server, it destroy.
How can i make a scheduled job for ever?

2 Answers

I took the liberty of editing your phrasing, since you're indeed not talking about cron jobs but a scheduled job within your Node.js process.

You can either

  • set up the machine's actual cron functionality to run that function in a stand-alone script at a schedule (arguably easiest)
  • set up the machine's actual cron functionality to start your script at boot (@reboot) (with the caveat that you are then still in charge of making sure you start your service if it shuts down)
  • set up the machine's service manager (likely to be Systemd these days) to run that function in a stand-alone script at a schedule (Systemd timers) (more modern than cron)
  • set up the machine's service manager (likely to be Systemd these days) to ensure your script keeps running at all times (Systemd units) (probably the most robust)
Related