Node JS: node-shedule parser bug - linux

This code does not work:
var schedule = require('node-schedule');
var j = schedule.scheduleJob('0,30 7-21 * * *', function(){
console.log(Date.now());
});
If I replace 7-21 with * it starts working. Why and how can I make it work from 7am to 9 pm?

Define your package.json file
run the follwoing commands.
npm install
npm install node-schedule
node <your js file/app.js>
(your sample code above works as expected)

Related

How can I replace node-cron with a command line script in a node.js application

I would like to change the implementation shown below to using a command line script in a node.js application. How do I do it?
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
getBananas();
});

How to run node.js code on the server automatically?

I am using a node package(sitemap-generator) to create a sitemap.xml file for my angular website,
At the moment I am running this code manually on my machine then uploading the generated file each time.
Is there any way I can automate this process, maybe by uploading the code and running it periodically and automatically
You can use node-scehdule module to run sitemap generator everyday at a specific time , you can set time according to your requirement
const SitemapGenerator = require('sitemap-generator');
const cron = require('node-schedule');
const generator = SitemapGenerator('https://examaple.com', {
maxDepth: 0,
filepath: '/var/www/example.com/sitemap.xml',
maxEntriesPerFile: 50000,
stripQuerystring: true,
lastMod:true
});
cron.schedule('0 22 * * *', () => {
console.log("will run at 10:00 PM everyday");
generator.start();
});

node-cron is not working on my server

So I'm using node-cron module.
I'm using this simple code and it should print go! everyday at 14:17.
I'm running the code in my local machine, everythings going well. But then I try to run the code on my server in DigitalOcean (Ubuntu 14), it never prints go! at 14:17. Nothing happen. Can you tell me what I've been missing?
var cron = require('node-cron');
var task = cron.schedule('0 17 14 * 1-12 0-7', function() {
console.log('go!');
}, false);
task.start();
Oh, by the way I tried running another code on my server and it works, print go! at 0 second
var cron = require('node-cron');
var task = cron.schedule('0 * * * 1-12 0-7', function() {
console.log('go!');
}, false);
task.start();
Check your server timezone and your local timezone and see if they match.
Reconfigure your server timezone with sudo dpkg-reconfigure tzdata

How do invoke script with another node script?

I have an node app that runs as a cron job every few seconds:
var CronJob = require('cron').CronJob;
new CronJob('*/5 * * * * *', function(){
console.log('Here invoke a script called requestdata');
}, null, true, "America/Los_Angeles");
and I just want to call a script without invoking functions on it. So its not
requestdata.foo();
but just call requestdata in same directory. how is this done? If it was on command line I would just do:
node requestdata
but how do I do this inside another script?
Use child_process, like so
var cp = require('child_process');
cp.fork(__dirname + '/request data.js');
See http://nodejs.org/api/child_process.html
This solution also shows how to pass parameters to the "request data.js" script
var cp = require('child_process');
cp.fork(__dirname + '/request data.js',[array,of,string,prams]);

Start gulp task from another node.js script

I'm using script like this:
run.js:
var gulp = global.gulp = require('gulp');
require('./gulpfile.js');
//interaction
gulp.start('zip');
gulpfile.js:
global.gulp = global.gulp || require('gulp');
gulp.task('zip', function () {});
And start: node run.js
I need it because I need collect some data via inquirer.prompt() before task start.
Everything works, but console freeze cursor after script end(in PHPStorm).
I don't understand why. If I run task via gulp, it's ok.
As mentioned by Aperçu in the comments, try letting gulp know that you're done your task.
Change
gulp.task('zip', function () {});
to
gulp.task('zip', function (done) {done()});

Resources