Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Cron Job every 45 minutes with Node-Cron

Tags:

node.js

cron

I'm using node-cron to run scheduled jobs. I want the jobs to run every 45 minutes, but its acting strangely

Here's the pattern I'm using

'00 */45 * * * *'

I started my script at Tue Jun 17 2014 08:17:39 GMT+0000 (GMT)

Here's are the first couple of times the job was executed

1. Tue Jun 17 2014 08:45:03 GMT+0000 (GMT)

2. Tue Jun 17 2014 09:00:01 GMT+0000 (GMT)

3. Tue Jun 17 2014 09:45:02 GMT+0000 (GMT)

This is definitely not what I expected or want. All I want is to run the Jobs every 45 minutes. Can anyone help me with the pattern? Thanks :)

like image 367
SamAko Avatar asked Jun 17 '14 09:06

SamAko


People also ask

How do I run a cron job every 20 minutes?

Instead of a range of values, you can also use the asterisk operator. To specify a job to be run every 20 minutes, you can use “*/20”.


6 Answers

You're probably looking for

0 */45 * * * *

The ranges are here.

  • Seconds: 0-59
  • Minutes: 0-59
  • Hours: 0-23
  • Day of Month: 1-31
  • Months: 0-11
  • Day of Week: 0-6
like image 85
Ben Fortune Avatar answered Oct 04 '22 17:10

Ben Fortune


I'm more familiar with cron than with node-cron, but I've taken a quick look at the documentation.

If I understand it correctly, node-cron uses a syntax similar to that used by cron, but with an additional "seconds" field. So where a cron job might have:

# min hour mday month wday command
*/15  *    *    *     *    some-command

to schedule some-command to run every 15 minutes, node-cron would use a similar syntax to specify the time to run:

'0 */15 * * * *'

(with an additional field to specify seconds), but it executes a specified JavaScript function, not an external command.

In standard cron, there is no syntax to specify running a job every 45 minutes. A specification of 0/45 * * * * would run a job twice each hour, at 0 and 45 minutes after the hour. To run a job every 45 minutes (at 00:00, 00:45, 01:30, 02:15, ..., i.e., 32 times per day) you'd have to schedule it to run every 15 minutes, and then invoke a script that checks the current time to decide whether to do anything.

Or you can write an exhaustive list of all the times you want the job to run:

 0  0 * * * some-command
45  0 * * * some_command
30  1 * * * some_command
15  2 * * * some_command
# 28 lines omitted

I'd definitely want to write a script to generate this list.

(This is workable because 24 hours happens to be a multiple of 45 minutes. You couldn't run something every 35 minutes this way.)

A similar approach should work for node-cron. Schedule the function to run every 15 minutes, and invoke a function that checks the current time to decide whether to run. For example, you can check whether the number of minutes since midnight modulo 45 is zero. (You might want to allow for a small variance in case the scheduling is not exact.)

I don't know JavaScript well enough to suggest the best way to write this function, but it should be reasonably straightforward.

Or write 32 lines to specify all the times you want it to run.

like image 41
Keith Thompson Avatar answered Oct 04 '22 17:10

Keith Thompson


There is no direct way to do this. However, we can get the result by intercepting the schedule using a shell command within the target script.

First, run the script at every 15 minutes:

*/15 * * * * <target_script>

Then, within the target_script, place the following code before actual codes:

#!/bin/sh

# Exit except at 0:45, 1:30, 2:15, 3:00, 3:45 etc

if ! echo "((`date +%-H`*60)+`date +%-M`)/45" | bc -l | grep "\.0*$" &> /dev/null;
then exit 1;
fi

# Your actual code goes here;
like image 32
Seff Avatar answered Oct 04 '22 19:10

Seff


I tried this string for a 45-second interval and it works well:

'*/45 * * * * *'
like image 37
SUBHASIS MONDAL Avatar answered Oct 04 '22 19:10

SUBHASIS MONDAL


You need to write a script as a wrapper to decide if the actual command shall be executed at every 45 minutes. That's 0, 45, 30 (= 45 + 45 - 60), 15 (= 30 + 45 - 60), 0 (= 15 + 45 - 60). so, the minutes to run the script shall be 0,15,30,45.

The command date +%M may be helpful in the shell script.

like image 40
Cody Avatar answered Oct 04 '22 19:10

Cody


you can use node-reel which is more readable, straight forward and awesome 😉.

const reel = require('node-reel')

reel().call(() => {
    console.log(Date.now());
}).everyFortyFiveMinutes().run()

https://github.com/shakee93/node-reel

like image 29
shakee93 Avatar answered Oct 04 '22 18:10

shakee93