简体   繁体   中英

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

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?

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)

You can use Crontab (native linux/unix program) utility to schedule your jobs. If you want to do it via node.js, you can use PM2 (simple and easy). Otherwise, you can create systemd script to start your program at startup.

https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM