CRONJOB does not belong to languages, belongs to the operating system, create a JOB from a script just run in the terminal:
crontab -e
00 * * * * /usr/bin/node /home/usuario/foo-bar.js
And you could use these commands with the native API:
Could do something like:
var exec = require('child_process').exec;
exec('crontab -e && 00 * * * * /usr/bin/node /home/usuario/foo-bar.js', function(error, stdout, stderr){
console.log(stdout);
});
I did not test, but it will probably work (the example is for linux servers, remembering to change folders according to locations)
To facilitate you can install via NPM the https://www.npmjs.com/package/cron
npm i cron.
Example of basic cron https://www.npmjs.com/package/cron#Usage-basic-cron-Usage:
var CronJob = require('cron').CronJob;
var job = new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');
job.start();
Note: Despite the example to suggest the Timezone, it would be better to work with UTC and study well on timezones, because many people use it in the wrong way.
yes. I have a Rest api
– Sérgio Avilla
React is front-end, which Backend is using, it can be configured this ...?
– novic
Gee, mds kkk I missed. I’m beginner at it. It’s in Node
– Sérgio Avilla
maybe it screwed up my research
– Sérgio Avilla
Dude, your backend is in Node, I recommend using Agenda to check some information from time to time
– Denis Rudnei de Souza
Thank you very much Virgilio and Denis. I have another question, if you could help me, I would be grateful again. I have there in my server the PM2 running the API, so I put this cron job to run, I can create it within the project itself? Like, create it in a worker folder and run it via pm2?
– Sérgio Avilla
You need to see if this cron job can send a request to your web api and with socket.io update your front ... that would be the initial logic
– novic