简体   繁体   English

如何在节点 js 中设置服务器重启后不会破坏的节点计划作业

[英]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.用户输入 cron 作业时间,它必须工作,直到用户取消它。 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.我冒昧地编辑了您的措辞,因为您确实不是在谈论 cron 作业,而是在您的 Node.js 进程中计划的作业。

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)设置机器的实际cron功能以按计划在独立脚本中运行该功能(可以说是最简单的)
  • 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)设置机器的实际cron功能以在启动时启动脚本( @reboot(但需要注意的是,如果服务关闭,您仍然负责确保启动服务)
  • 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)设置机器的服务管理器(现在可能是 Systemd)以按计划在独立脚本中运行该功能(Systemd 计时器) (比 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)设置机器的服务管理器(现在可能是 Systemd)以确保您的脚本始终运行(Systemd 单元) (可能是最健壮的)

You can use Crontab (native linux/unix program) utility to schedule your jobs.您可以使用 Crontab(本机 linux/unix 程序)实用程序来安排您的作业。 If you want to do it via node.js, you can use PM2 (simple and easy).如果你想通过 node.js 来做,你可以使用 PM2(简单易行)。 Otherwise, you can create systemd script to start your program at startup.否则,您可以创建 systemd 脚本以在启动时启动您的程序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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