Using node-schedule to schedule at 0th and 30th minute - node.js

I'm trying to come-up with a cron expression (using node-schedule) which will help me schedule a function on the 0th and 30th minute of the hour. Whatever time we start the app, it must run at the 0th minute and 30th minute of the hour.
This would work but i don't think the rule regarding 0th and 30th minute would work!
schedule.scheduleJob('* */30 * * * *', runFunc);
Below recurrence rule would run every hour at 30 minutes after the hour, but is it possible to change this to include even 0th minute?
var rule = new schedule.RecurrenceRule();
rule.minute = 30;

You may just remove the wildcard for seconds expression otherwise you get function calls every second during the minute is at 0th and 30th minutes. Rest of the code should be fine. (#)
Thus this should work:
schedule.scheduleJob('0 */30 * * * *', () => console.log(new Date()));
Here is the debug code for your reference (checking input at 0th and 30th seconds)
var schedule = require('node-schedule')
schedule.scheduleJob('* * * * * *', () => console.log("now at " + new Date()));
schedule.scheduleJob('*/30 * * * * *', () => console.log(new Date()));
(#) I edited my answer that was previously 0,*/30 however noticed that you just need */30 to get 0th and 30th minutes calls.

Related

Execute a script every random number with nodejs

I want to execute a script with a random interval between 11 to 13 minutes. I tried this code, but the script is execute in much less than 11 minutes. Is my calculation wrong?
setInterval(function() {
// code to be executed
const script = spawn('node', ['index.js']);
}, 1000 * 60 * Math.floor(Math.random() * 3) + 11); // 1000 milliseconds * 60 seconds * random number of minutes between 11 and 13
First thing, you are missing parenthesis.
It should be 1000 * 60 * (Math.floor(Math.random() * 3) + 11) instead of 1000 * 60 * Math.floor(Math.random() * 3) + 11)
Secondly, Math.floor(Math.random() * 3) can only take 3 possible values (0, 1 or 2), so your script will run either every 11, 12 or 13 minutes.
If you want more possible values between 11 and 13 minutes, you can compute the time interval this way:
Math.floor(1000 * 60 * (Math.random() * 3 + 11)) to have many possible values between 11 and 13 minutes.
This will do the job:
setInterval(function() {
// code to be executed
const script = spawn('node', ['index.js']);
}, Math.floor(1000 * 60 * (Math.random() * 3 + 11)); // 1000 milliseconds * 60 seconds * random number of minutes between 11 and 13

time pattern in node-cronjob

I am using node-cron. Please help to explain to me the different between:
var pattern_1 = '58 * * * * *';
var pattern_2 = '*/58 * * * * *';
when running this function:
new CronJob(pattern, function() {
console.log('lalalalala')
}, null, true, 'America/Los_Angeles');
As described in cron man page:
Step values can be used in conjunction with ranges. Following a range
with ''/'' specifies skips of the number's value through the
range.
and:
Steps are also permitted after an asterisk, so if you want
to say ''every two hours'', just use ``*/2''.
So:
var pattern_1 = '58 * * * * *';
executes "at 58th seconds of every minute". The second pattern:
var pattern_2 = '*/58 * * * * *';
executes "every 58 seconds".
The first patterns will run your cronjob every 58th second: 00:00:58, 00:01:58, 00:02:58...and so on.
The slash character can be used to identify periodic values. For example */15 * * * * * means, that your job will run ever 15th second: 00:00:15, 00:00:30, 00:00:45 ...and so on.
In my opinion */58 doesn't look very useful. This will execute every 58th second of every minute, so just use the first one.
The second pattern:
var pattern_1 = '58 * * * * *';
It executes at "58th seconds of every minute".
The second pattern:
var pattern_2 = '*/58 * * * * *';
Same as pattern 1 so it also executes at "58th seconds of every minute".

Node cron, run every midnight

I want to run cron job daily at midnight. For this I am using
0 0 0 1-31 * *
but it doesn't work for me.
I am using the node cron. Please suggest the valid format.
You don't need to set all the fields. Set just first three and it'll take care of running every day at midnight
0 0 0 * * *
It's quite simple....
The below is the code to run crone job every day 12 AM..
var job = new CronJob('0 0 0 * * *', function() {
//will run every day at 12:00 AM
})
For more https://www.npmjs.com/package/cron
You can try this format too.
var CronJob=require('cron').CronJob;
var cronJob1 = new CronJob({
cronTime: '00 00 00 * * * ',
onTick: function () {
//Your code that is to be executed on every midnight
},
start: true,
runOnInit: false
});
To understanding something more about cronTime, See the following codes:
cronTime: '00 */3 * * * * ' => Executes in every 3 seconds.
cronTime: '* */1 * * * * ' => MEANING LESS. Executes every one second.
cronTime: '00 */1 * * * * ' => Executes every 1 minute.
cronTime: '00 30 11 * * 0-5 ' => Runs every weekday (Monday to Friday) # 11.30 AM
cronTime: '00 56 17 * * * ' => Will execute on every 5:56 PM
Here is:
var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 00 * * *', function() {
/*
* Runs every day
* at 00:00:00 AM.
*/
// DO SOMETHING
}, function () {
/* This function is executed when the job stops */
},
true /* Start the job right now */
);
0 0 * * *
This pattern will run CronJob job daily at 00:00
https://crontab.guru/examples.html

Cron : Setting alternative seconds

I posted a question the other day about setting alternative minutes in cron, and i was given a lovely simple answer.
0-59/2 * * * * first_script
1-59/2 * * * * second_script
This worked brilliantly, however i have seen realized that i need my scripts to run quicker than every minute.
I know cron doesn't support seconds, but you can bluff it by using sleep, like so
* * * * * /foo/bar/your_script
* * * * * sleep 15; /foo/bar/your_script
* * * * * sleep 30; /foo/bar/your_script
* * * * * sleep 45; /foo/bar/your_script
So i need to combine the both of these so that i can get them to run alternatively every 15 seconds for instance.
Any ideas?
Ended up with the following code to get my scripts to run in shorter intervals than 1 minute.
* * * * * /usr/bin/php -q /path/to/file/script1.php
* * * * * sleep 15; /usr/bin/php -q /path/to/file/script2.php
* * * * * sleep 30; /usr/bin/php -q /path/to/file/script1.php
* * * * * sleep 45; /usr/bin/php -q /path/to/file/script2.php

Run two operations with different frequency in Bash

I need a script in bash to run two applications with different frequency. I'm not that experienced with bash and need some help
I have two programs, m1 and m2, to be run at different rates over time (stress test). m1 is for example run every 10 seconds and m2 every 30 seconds. But it should be possible to change the frequency
To simplify a couple of other answers:
$ while sleep 10; do echo 1; done &
$ while sleep 30; do echo 2; done &
Note that if your "m1" and "m2" commands take time to execute, you won't be running them every 10/30 seconds. The sleep is the delay between the end of one run and the start of the next.
So if you really want to schedule these so they run every 10 or 30 seconds, use cron. Cron runs once per minute, so you need to have multiple cron jobs, offset with sleep:
* * * * * m1
* * * * * sleep 10; m1
* * * * * sleep 20; m1
* * * * * sleep 30; m1
* * * * * sleep 40; m1
* * * * * sleep 50; m1
* * * * * m2
* * * * * sleep 30; m2
Note that if m1 takes more than 10 seconds to run, you'll overlap, which may cause your computer to vanish into a quantum singularity.
$ while true; do sleep 10; echo 1; done &
$ while true; do sleep 30; echo 2; done &
1
1
2
1
1
1
2
...
I think what you are looking for is the sleep command in combination with a while true loop.
while true; do m1;sleep 10;done
while true; do m2; sleep 30;done

Resources