161

Is there some cron like library that would let me schedule some function to be ran at certain time (15:30 for example, not x hours from now etc)? If there isn't this kind of library how this should be implemented? Should I just set callback to be called every second and check the time and start jobs scheduled for the time or what?

JtR
  • 20,568
  • 17
  • 46
  • 60

11 Answers11

125

node-cron does just what I described

JtR
  • 20,568
  • 17
  • 46
  • 60
  • 5
    do you write the cronjob declarations in app.js/scripts.js? – ArVan Dec 06 '12 at 09:05
  • does **node-cron** allow you to edit system cron jobs from node.js? Is it dependent on the node process running? or will it allow you to run programs even after your main process terminates? – Ajar Mar 05 '14 at 05:27
  • 6
    @Ajar no, after termination of nodejs process, all jobs will be lost. There is no cron table shipped with node-cron – DarkLeafyGreen Apr 24 '14 at 10:06
  • @ArVan I know this is a couple years later but did you find that app.js/scripts.js was the best place to declare your cronjobs? – ChrisC Sep 21 '14 at 17:26
  • 2
    I guess so @ChrisC. If I'm not using any framework, I declare crons in `app.js`. In `sails.js` I do it in `bootstrap` – ArVan Sep 22 '14 at 13:02
  • @ArVan Thanks, I actually ended up separating it into it's own cron.js file to try to keep it as modular as possible. I will have a lot going on in there so probably best not to polute my app.js in this particular instance. – ChrisC Sep 23 '14 at 13:47
  • 2
    @arVan if you're interested, I actually started working on `agenda` to add Waterline support here: https://github.com/mikermcneil/agenda (literally just started though- should have some usable progress by next week) – mikermcneil Sep 26 '14 at 03:30
  • Some of the more popular packages compared: https://npmcompare.com/compare/agenda,cron,later,node-runnr,node-schedule – steampowered Aug 01 '17 at 01:14
  • I faced issue with node-cron that it calls twice. I couldn't get rid of it. – suresh Jan 09 '19 at 21:24
45

node-schedule A cron-like and not-cron-like job scheduler for Node.

Unitech
  • 5,781
  • 5
  • 40
  • 47
  • 2
    This manages to avoid the sometimes confusing crontab syntax, and is a little more readable in my opinion. – Simon East Mar 31 '13 at 06:20
  • like agenda it seams to provide persistence 0.1.13 – drdrej Feb 06 '14 at 15:17
  • 5
    node-schedule does not provide persistence. Per the docs: `Note that node-schedule is designed for in-process scheduling, i.e. scheduled jobs will only fire as long as your script is running, and the schedule will disappear when execution completes.` – Mike Hedman Jan 10 '15 at 14:04
37

agenda is a Lightweight job scheduling for node. This will help you.

Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
  • 1
    agenda is based on node-cron with a persisten layer (mongo). so the answer above is better. – drdrej Feb 06 '14 at 14:57
  • 53
    To clarify, as the author of agenda, it is actually not based on node-cron at all... – Ryan May 29 '14 at 18:20
  • I recently wrote a blog comment about an agenda setup. Also it suggests a project structure http://goo.gl/5sCNBM – Robin Wieruch Sep 01 '14 at 09:24
  • 1
    The agenda library is the one and only best library as the setup is simple and usage is easy for the user. Kudos @Ryan – Vimalraj Selvam Oct 27 '15 at 05:28
  • 5
    I've found `agenda` to have inconsistent behaviour and would not advise using it. Check the open issues and PRs to make your own call. – Russbear Aug 03 '16 at 15:36
  • 2
    be aware, agenda is not production ready. It has [memory leak](https://github.com/agenda/agenda/issues/129) issues and check for issues. Still has a lot of bugs. It looks promising when ready. – Sebastian Aug 22 '17 at 05:09
  • regarding "lightweight", according to its dependencies it's not so light... for example: unpacked size of its dependencies - moment-timezone 1.34 MB + moment 4.09 MB + mongodb 1.45 MB ... Although other scheduler packages may have similar deps. – Yury Kozlov Jun 02 '20 at 20:59
15

later.js is a pretty good JavaScript "scheduler" library. Can run on Node.js or in a web browser.

XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151
  • 3
    I tried a few of the suggestions in this thread, but this module I found to be the most *user-friendly* and straight fwd to work with .. thanks! – Gene Bo Sep 14 '15 at 17:39
  • For anybody coming around this lib - [here](https://breejs.github.io/later/index.html) is a maintained fork of it. – Naz Nov 05 '20 at 00:55
12

I am using kue: https://github.com/learnboost/kue . It is pretty nice.

The official features and my comments:

  1. delayed jobs.
    • If you want to let the job run at a specific time, calculate the milliseconds between that time and now. Call job.delay(milliseconds) (The doc says minutes, which is wrong.) Don't forget to add "jobs.promote();" when you init jobs.
  2. job event and progress pubsub.
    • I don't understand it.
  3. rich integrated UI.
    • Very useful. You can check the job status (done, running, delayed) in integrated UI and don't need to write any code. And you can delete old records in UI.
  4. infinite scrolling
    • Sometimes not working. Have to refresh.
  5. UI progress indication
    • Good for the time-consuming jobs.
  6. job specific logging
    • Because they are delayed jobs, you should log useful info in the job and check later through UI.
  7. powered by Redis
    • Very useful. When you restart your node.js app, all job records are still there and the scheduled jobs will execute too!
  8. optional retries
    • Nice.
  9. full-text search capabilities
    • Good.
  10. RESTful JSON API
    • Sound good, but I never use it.

Edit:

  1. kue is not a cron like library.
  2. By default kue does not supports job which runs repeatedly (e.g. every Sunday).
Vince Yuan
  • 10,533
  • 3
  • 32
  • 27
  • 6
    Kue is a job queue, not a scheduler. – Leonid Beschastny Dec 21 '13 at 17:03
  • 1
    @LeonidBeschastny Kue is a job queue. And it is a scheduler too, because it can "let me schedule some function to be ran at certain time". – Vince Yuan Dec 23 '13 at 14:43
  • I thought Kue jobs can only be delayed, but not scheduled to a certain time. – Leonid Beschastny Dec 23 '13 at 15:25
  • @LeonidBeschastny You are right. Kue does not provide api to schedule a job to a certain time. But as I mentioned, "If you want to let the job run at a specific time, calculate the milliseconds between that time and now. Call job.delay(milliseconds) (The doc says minutes, which is wrong.)" It's very easy. – Vince Yuan Dec 23 '13 at 15:54
  • 1
    You could try kue-scheduler, which is similar to aganda – Fruch Oct 26 '16 at 05:05
10

You can use timexe

It's simple to use, light weight, has no dependencies, has an improved syntax over cron, with a resolution in milliseconds and works in the browser.

Install:

npm install timexe

Use:

var timexe = require('timexe');
var res = timexe("* * * 15 30", function(){ console.log("It's now 3:30 pm"); });

(I'm the author)

Simon Rigét
  • 2,757
  • 5
  • 30
  • 33
9

node-crontab allows you to edit system cron jobs from node.js. Using this library will allow you to run programs even after your main process termintates. Disclaimer: I'm the developer.

Blago
  • 4,697
  • 2
  • 34
  • 29
3

I am the auhor of node-runnr . It have a very simple approach to create job. Also its very easy and clear to declare time and interval. For example, to execute a job at every 10min 20sec,

Runnr.addIntervalJob('10:20', function(){...}, 'myjob')

To do a job at 10am and 3pm daily,

Runnr.addDailyJob(['10:0:0', '15:0:0'], function(){...}, 'myjob')

Its that simple. For further detail: https://github.com/Saquib764/node-runnr

codedemon
  • 84
  • 4
2

All these answers and noone has pointed to the most popular NPM package .. cron

https://www.npmjs.com/package/cron

danday74
  • 52,471
  • 49
  • 232
  • 283
1

Both node-schedule and node-cron we can use to implement cron-based schedullers.

NOTE : for generating cron expressions , you can use this cron_maker

dush88c
  • 1,918
  • 1
  • 27
  • 34
0

This won't be suitable for everyone, but if your application is already setup to take commands via a socket, you can use netcat to issue a commands via cron proper.

echo 'mycommand' | nc -U /tmp/myapp.sock
Adria
  • 8,651
  • 4
  • 37
  • 30