Skip to content

How can I start the job immediately and after run on a interval #776

Answered by sheerlox
tungrix asked this question in Q&A
Discussion options

You must be logged in to vote

As specified in the API documentation, you can use the runOnInit option to meet that requirement:

import { CronJob } from 'cron';

const job = new CronJob(
  '*/5 * * * * *', // cronTime
  function () {
    console.log('You will see this message every 5 second');
  }, // onTick
  null, // onComplete
  true, // start
  'America/Los_Angeles', // timeZone
  null, // context
  true // runOnInit
);

On a side note, because it is more explicit and does not care about the order of options, I would recommend using the CronJob.from() static method to declare your jobs:

import { CronJob } from 'cron';

const job = CronJob.from({
  cronTime: '*/5 * * * * *',
  onTick: function () {
    console.log('Y…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@tungrix
Comment options

@sheerlox
Comment options

Answer selected by sheerlox
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants