Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs cron plugin vs running nodejs script from crontab

I'm building a tool where users can enter a number of items they are interested in. Every 24 hours I want to run a script that checks certain JSON responses from external sources for these topics.

My question is: why would you make a script and run it using crontab rather than making a module using the node-cron plugin and include it in your app.js file. Or would you never do this?

Basically want to go for best practice on this one.

like image 278
Jeffrey Vandenborne Avatar asked Jun 08 '13 15:06

Jeffrey Vandenborne


People also ask

How do I run a node js script in crontab?

Run Node.js script in cron //exceute every 1 mincron. schedule('*/1 * * * *', function(){var shell = require('./child_helper'); var commandList = \[ "node script1. js", "npm run script -- PeterGood" \] shell.

What is node-cron in Nodejs?

The node-cron module is tiny task scheduler in pure JavaScript for node. js based on GNU crontab. This module allows you to schedule task in node. js using full crontab syntax.

How do I run multiple scripts in crontab?

Multiple Commands in the Same Cron Job We can run several commands in the same cron job by separating them with a semi-colon ( ; ). If the running commands depend on each other, we can use double ampersand (&&) between them.

Is node-cron blocking?

First, node-cron has the same merits and demerits as Node. js, being a runtime of JavaScript, which happens to be a non-blocking single-threaded language that uses the event loop.


1 Answers

The main difference between the two methods in my opinion would be the level at which you want to schedule job. When using crontab your jobs are scheduled by the cron daemon that runs on the system.

node-cron on the other hand is a pure JavaScript implementation of cron. So system is not responsible for running jobs but your V8 engine which executes it. Jobs will be run as long as your js application runs.

So why would you use one or other ?

That depends on the purpose of your job, where is it best tethered . If job is a maintenance job for system run it via crontab. If you want to run a function in node.js periodically use node-cron. If you want to run a bash script you would want to use crontab. So how you want to do it via system (bash) or JavaScript is upto you.

like image 124
user568109 Avatar answered Sep 18 '22 21:09

user568109