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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With