How to schedule the node js script to run automatically on windows? - node.js

This script
0 0/3 * * * node test.js
is used to schedule the job in Ubuntu, How to set the same way in Windows using node-schedule npm package?
As a work around , have scheduled the script in Task Scheduler:
cmd /c c:\node\node.exe c:\myscript.js
I want to to know how this can be done in node-schedule npm package.

You can use 'cron' package for schedule functions on nodejs -
https://www.npmjs.com/package/cron
According it's docs
Cron is a tool that allows you to execute something on a schedule.
This is typically done using the cron syntax. We allow you to execute
a function whenever your scheduled job triggers.
Usage example
const CronJob = require('cron').CronJob;
const exampleJob = new CronJob(`*/2 * * * * *`,()=>{
console.log("You will see this message every 2 seconds",new Date().getSeconds());
});
exampleJob.start();
for addition if you not familiar with cron schedule expressions(cron syntax) that web will help you get right expression
https://crontab.guru/

From https://npmjs.org/package/node-schedule:
Execute a cron job every 5 Minutes = */5 * * * *
So (according to the NodeJS docs) you can use the child_process npm module to run the script.
Like this:
const { spawn } = require('child_process');
const schedule = require('node-schedule');
schedule.scheduleJob('*/5 * * * *', function() {
spawn('node', ['test.js']);
});

Related

How to make the node js code always run even if the system is shutdown

i have created the cron job using node js ,which have to run in the interval of every hour even if the system is shut down. It has to run automatically with out manual invoke of the code .Please help to solve this
i have tried with pm2 but not working as expected.
First you should install npm package for crone-job
npm i node-cron
And then you can follow this code for your automatic job
const schedule = require("node-cron");
schedule.scheduleJob("00 02 * * *",
function () {
console.log("The answer to life, the universe, and everything!");
expected_Function_Name();
});
You will get your function output in every day 2pm.

Node cron not executed on production

I use a web server (o2Switch) who use a Cpanel interface, my nodejs app is running with the setup node.js module.
My rest api use Nodejs with express and a database mongoDb atlas, my front is a Vuejs app, everythings works fine in production, but i have one cron with node-cron package:
// scripts/myscript.js
const cron = require('node-cron');
const script = cron.schedule('00 20 * * *', async () => {
console.log('PASSED')
},
{
scheduled: true,
timezone: "Europe/Paris"
}
);
script.start()
The script is import inside app.js:
// Scripts
require('./scripts/myscript')
My nodecron works fine in local and i don't have any trouble, but on production is not executed.
I do a lot of test:
It's 10:00(am) and i setup my cron on 05 10 * * *, this will work. But is never reexecuted the following day.
It's 10:00(am) and i setup 00 12 * * * this will never works.
I setup every minute * * * * * this works every minute.
What is seems, is my server is like standby after a couple of hours (i don't know if it's possible).
My vuejs app works fine at any time, the request works and do not have trouble.

Run node script in cron job

I am trying to run a node script with cron every minute. I dont believe it is running at all. When I run grep cron /var/log/syslog I dont see it running in the log.
Did I write the cron job wrong? If so how do I run a node script in a cron job?
* * * * * node /home/ubuntu/Server/nodeScript.js
Cron jobs don't run in a shell so you have to give the full path to the node binary.
/usr/bin/node (or whatever it is on that machine)
Try which node to find out
The easiest way to run it every single minute is just to put your code in endless loop with timeout.
For example
function yourCode () {
for {
// your code
timeout(60 * 1000)
}
}
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Just be sure you run it with pm2 or supervisor so when it crashes it can restart automatically.

How can I run my node js script automatically using scheduler on server

I have created a nodejs file using express enviornment and running the file on server using nodemon. Currently I have to give commands to the interface to run the particular file on nodemon but what I currently need is to schedule the task to run that file on server automatically at multiple occasion in a single day.
my file excute like this on terminal::
nodemon example_api.js
output terminal:
root#*********:/var/www/example project# nodemon example_api.js
[nodemon] ##.##.#####
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node api.js`
Listening on port 8080
Note: I am currently running node js on Mobaxterm terminal currently using windows but my file will be run on a server with linux interface
1. If you want to run your node process continuously and want to run only particular task:
Use node-schedule or node-cron packages to run your code block at desired time or interval.
i.node-schedule
var schedule = require('node-schedule');
var j = schedule.scheduleJob('*/30 * * * * ', function(){
console.log('The answer to life, the universe, and everything!');
});
ii.node-cron
var cron = require('node-cron');
cron.schedule('*/30 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
2. If you want to run only single node script:
You can use Linux crontab to execute your script at desired time
crontab -e
and add following entry
*/30 * * * * /usr/local/bin/node /home/ridham/example/script.js
This will execute /home/ridham/example/script.js every 30 minutes. and always give full qualified path here.
You have to give crontime in any of the following. you can learn about crontime here
For a simple implementation - you can just use setInterval like so:
setInterval(() => {
// do something every 5 seconds
}, 5000);
But it you want something like a cron, then, you might wanna go with node-cron or node-schedule.
You can also use providers like AWS CloudWatch. AWS CloudWatch allows you to run AWS Lambda functions on a cron.

Cronjob not running when restart app or server

I have Cronjob use node_module: node-cron
var hello = function( ) {
console.log('Hello world')
}
var job=new CronJob({cronTime:'00 52 13 * * *',
// that's important (100% made up example):
onTick: function() {
hello({
done: true,
lastRun: new Date()
});
},
start:false,timeZone:"America/Los_Angeles"});
job.start();
when I restart server .Cronjob not running.
node scripts that uses node-cron still have to be started for the cronjob to run. Adding things to node-cron, does not add it to cron on your machine, even though the naming can be confusing. node-cron simply allows your script to schedule jobs inside your script, while your script is running.
For this to work, either convert to scheduling the script on your machine, using the built in scheduler, or start your node script on machine startup. (This is different depending on your operating system).

Resources