Cron作业可以每隔'x'秒运行一次吗 [英] Can a cron job run every 'x' seconds

查看:266
本文介绍了Cron作业可以每隔'x'秒运行一次吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cron作业设置,最小值为60秒,并且我希望程序能够以第二个间隔运行,即无论我将其设置为60秒钟以上。

I have a cron job setup, with the minimum value of 60 seconds, and I want the program to be able to run at second intervals i.e. whatever I set it as 60 seconds onwards.

例如,我希望cron作业每65秒,每63秒或每160秒等运行一次。

So for example, I want the cron job to run every 65 seconds, or every 63 seconds, or every 160 seconds etc.

这可能吗?还是cron作业仅设置为以60秒的间隔运行?

Is this possible? Or is cron job only set to run at 60 second intervals?

如果是,那么cron作业的外观将是什么样?

If yes, what would the cron job for this look like?

我设法使它每隔'x'分钟运行一次:
到目前为止,我已设法使用用户插入的数据库值来每次运行cron作业设置分钟数。看起来像这样:

I managed to make it run every 'x' minutes: So far, I have managed to use a database value which the user inserts to run the cron job every set number of minutes. Which looks like this:

  cron.schedule('*/' + test + ' * * * *', function () {
    console.log('running a task every minute');
  });

模式如下:

var CronSchema = new mongoose.Schema({
  minutes: { type: Number, min: 1 }
})

我如何每隔'x'秒执行一次cron作业?
理想情况下,我只允许一个用户输入以秒为单位的值,因此,如果他们希望作业每两分钟运行一次,则必须输入120秒。

How can I run a cron job every 'x' seconds? Ideally, I would only allow a user to input a value in seconds, so if they want the job to run every two minutes they would have to input 120 seconds.

推荐答案

如果您将中间件用于Node,也可以添加秒数

If you're using middleware for Node, you can add seconds as well

cron.schedule('*/' + test + '0,30 * * * * *', function () {
    console.log('running a task every minute');
});

每30秒就会运行一次,即 xx:00:000 xx:30:000

Will run every 30 seconds, i.e. on xx:00:000 and xx:30:000

请注意,javascript版本具有六个值,而不是五个像本地Linux Cron一样,意味着第六个是几秒钟,这将是第一个

Note that javascript versions have six values, and not five like native Linux Cron, meaning the sixth is for seconds, which would be the first one

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

完整文档有关允许的值的信息,请参见 crontab文档

Full documentation on allowed values can be found in the crontab documentation

这篇关于Cron作业可以每隔'x'秒运行一次吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆