DJAMEL DAHMANE
DJAMEL DAHMANE

Reputation: 424

Add a job to linux cron list programmatically with Nodejs

I have a job with Nodejs that I want to do it each 30 minutes to scan the Database and Update Products Data in An Ecommerce API with my Nodejs Program, note that the Nodejs Program is serving an REST API (Backend) for a react js web application So I searched for that and I found that I can do that with Nodejs Cron Library like "node-schedule" but I know that will be more interesting to do it with Linux Cron

 var j = schedule.scheduleJob('42 * * * *', function(){
   console.log('The answer to life, the universe, and everything!');
  }); 

Is there any library that can let me add Cron jobs to Linux using Nodejs Or would I do it with "fs" only? so I will open the cron job file and add my command?

Upvotes: 0

Views: 701

Answers (2)

tenbits
tenbits

Reputation: 8008

You can put your cron job to a nodejs script. Then adding to the crontab can be done with cronbee module, via API:

import { cronbee } from 'cronbee'
await cronbee.ensure({
    taskName: 'do smth',
    taskRun: `node my-script`,
    cron: '42 * * * *'
})

or you can ensure the cron job via CLI, if the module is installed globally or from npm scripts:

$ cronbee ensure mytasks.json

Upvotes: 0

Zan Lynx
Zan Lynx

Reputation: 54325

The command crontab which is part of Vixie Cron allows you to create, edit and delete per-user cron entries.

Or if you are running as the root user, which you should not be doing, you can drop cron files into /etc/cron.d

This is not always supported, and if you're running in a Docker type container environment it is doubtful that you have any cron at all. In that environment you'd want your running Nodejs to handle scheduled jobs for you. Or use some other kind of distributed scheduled work system.

Upvotes: 1

Related Questions