Can a cron job run every 'x' seconds

后端 未结 2 787
我寻月下人不归
我寻月下人不归 2021-02-14 08:22

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.

相关标签:
2条回答
  • 2021-02-14 08:27
    cron.schedule('*/' + test + '0,x * * * * *', function () {
       console.log('running a task every x seconds');
    });
    

    this will run every x seconds.

    0 讨论(0)
  • 2021-02-14 08:34

    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');
    });
    

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

    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)
    

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

    0 讨论(0)
提交回复
热议问题