46
walk.on('dir', function (dir, stat) {
    uploadDir.push(dir);
});

I am using Node, and i need make this function run everyday at midnight, this is possible?

  • possible duplicate of [Is there a job scheduler library for node.js?](http://stackoverflow.com/questions/3785736/is-there-a-job-scheduler-library-for-node-js) – Adam Jenkins Oct 10 '14 at 18:52
  • i try use, but it appeared that there was a memory leak –  Oct 10 '14 at 18:53
  • 1
    Your comment makes no sense, there are 6 different npm packages listed in that thread. Did they all have memory leaks? – Adam Jenkins Oct 10 '14 at 18:56

4 Answers4

98

I believe the node-schedule package will suit your needs. Generally, you want so-called cron to schedule and run your server tasks.

With node-schedule:

import schedule from 'node-schedule'

schedule.scheduleJob('0 0 * * *', () => { ... }) // run everyday at midnight

Installation:

npm i node-schedule --save
mvermand
  • 5,829
  • 7
  • 48
  • 74
wap300
  • 2,660
  • 1
  • 16
  • 25
  • 2
    Thanks! I don't know why, but this package is working and the others are not. <3 –  Oct 10 '14 at 19:12
  • There are some other similar node packages as well: https://npmcompare.com/compare/agenda,cron,later,node-schedule – steampowered Aug 01 '17 at 01:03
  • do we need to start it everyday ? or automatically start ? – yogesh agrawal Jan 17 '18 at 05:58
  • Caution, node-schedule may be heavyweight for your needs. I just discovered our project uses it in only one place at setTimeout would work, and it adds 1.1MB minified JavaScript (!) in v1.3.2. Granted, v2 is about 10% of the size. Anyway, it is worth considering setInterval/setTimeout if it can be used. – ryanm Aug 02 '21 at 21:04
30

I use the following code:

function resetAtMidnight() {
    var now = new Date();
    var night = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate() + 1, // the next day, ...
        0, 0, 0 // ...at 00:00:00 hours
    );
    var msToMidnight = night.getTime() - now.getTime();

    setTimeout(function() {
        reset();              //      <-- This is the function being called at midnight.
        resetAtMidnight();    //      Then, reset again next midnight.
    }, msToMidnight);
}

I think there are legitimate use-cases for running a function at midnight. For example, in my case, I have a number of daily statistics displayed on a website. These statistics need to be reset if the website happens to be open at midnight.

Also, credits to this answer.

Community
  • 1
  • 1
TinkerTank
  • 5,685
  • 2
  • 32
  • 41
  • 1
    I like this answer, however it has an issue: it does not get the right date when crossing months or even years. You might want to replace the `night` definition with `let night = now; night.setDate(new Date().getDate()+1); night.setHours(0, 0, 0)`. That solves this issue. – tukusejssirs Jan 22 '21 at 11:21
22

There is a node package for this node-schedule.

You can do something like this:

var j = schedule.scheduleJob({hour: 00, minute: 00}, function(){
    walk.on('dir', function (dir, stat) {
       uploadDir.push(dir);
    });
});

For more info, see here

Donal
  • 31,121
  • 10
  • 63
  • 72
4

Is this a part of some other long-running process? Does it really need to be? If it were me, I would just write a quick running script, use regular old cron to schedule it, and then when the process completes, terminate it.

Occasionally it will make sense for these sorts of scheduled tasks to be built into an otherwise long-running process that's doing other things (I've done it myself), and in those cases the libraries mentioned in the other answers are your best bet, or you could always write a setTimeout() or setInterval() loop to check the time for you and run your process when the time matches. But for most scenarios, a separate script and separate process initiated by cron is what you're really after.

Jason
  • 13,606
  • 2
  • 29
  • 40
  • i'm having some problems, i can't use a schedule package or setTimeout()... the both is not working.. –  Oct 10 '14 at 19:03
  • @Nescau - why can't you use a schedule package or `setTimeout()`. Both should work fine. You have to help us help you. – jfriend00 Oct 10 '14 at 20:22
  • The question isn't whether the scheduled task takes a long time, it's whether you need it to be part of a larger containing persistent process, or if it's otherwise self contained. Do you need node to be running and doing something else in preparation for your scheduled task to run at midnight? Or can you just spin up your process at midnight and then terminate it when it's done, just to spin it up again at the next midnight? Generally these sorts of things fall into the latter category. – Jason Oct 13 '14 at 20:10