Cron jobs in Node.js

Alex Vasilev
Geek Culture
Published in
4 min readJun 7, 2021

--

Every developer sooner or later faces a problem on how to deal with cron jobs. The list of cron jobs use-cases is enormous: cache invalidation, reports generation, refreshing data from external sources, notifications, backups etc. And of course my favourite one: restarting services with memory leak every night :) Today we’ll walk through several scheduled tasks implementations from development and infrastructure perspectives.

Historically Cron jobs have a special format for schedule definition https://en.wikipedia.org/wiki/Cron. I find it kinda funny that we’re still using the format developed in the 70’s, it’s not intuitive at all. But don’t be afraid of it, there’re a lot of online tools, which translate asterisks, slashes and numbers to human readable format. For example this one: https://www.freeformatter.com/cron-expression-generator-quartz.html.

OK, enough lyrics, let’s move to practice!

Old school way (Linux approach)

Every Linux machine has cron daemon installed and running. So you just need to write a script for your task, which performs all required operation and exits, and then add it to cron with Ansible or Puppet. It’s a classic…

--

--