Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement