Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to Become a Product Designer: A Complete Guide for Career-Changers and Tech Pivoters

    September 30, 2025

    Socratic Questioning: A Powerful Tool for Deeper Learning and Critical Thinking

    September 28, 2025

    How to Become an AI Engineer: A Complete Guide for Career-Changers and Tech Pivoters

    September 27, 2025
    Facebook X (Twitter) Instagram
    The BridgeTechAcademy BlogThe BridgeTechAcademy Blog
    • Home
    • About
    • Courses
    • Testimonials
    • Blog
    • Contact
    The BridgeTechAcademy BlogThe BridgeTechAcademy Blog
    Home » Automating Tasks with Cron Jobs using Node-Cron
    Technology

    Automating Tasks with Cron Jobs using Node-Cron

    Kaosarat Tolani BadmusBy Kaosarat Tolani BadmusSeptember 27, 2025No Comments4 Mins Read
    Facebook Twitter LinkedIn Copy Link
    Share
    Facebook Twitter LinkedIn Copy Link

    Many routine tasks need to happen on time, whether it’s sending updates, organizing files, or cleaning up old data. When managed manually, these steps are prone to errors or delays. Automation ensures they are carried out consistently and with minimal effort. In software systems, one of the ways to automate these routine tasks is through a cron job.

    images of clocks illustrating the concept of schedules.

    A cron job is a powerful scheduling tool that automates consistent and repetitive tasks or processes in systems. You can think of it as your digital assistant that never forgets and never sleeps.
    Many of us have used “schedule payment” features in our banking or payment applications, this is an example of cron job technology at work. When you set up automatic bill payments or recurring transfers, you are leveraging the same principles that power cron jobs.

    Cron jobs eliminate the need for manual intervention in repetitive processes. They ensure that important tasks get executed precisely when needed without the risk of human error or forgetfulness. This automation allows systems to run efficiently while freeing up valuable time and mental bandwidth for more strategic work.

    This article introduces the concept of cron jobs using Node.js and the node-cron library.

    Prerequisites

    Npm

    Node.js 

    Implementation Steps

    Step 1: Initialise a new project

    npm init -y

    Step 2: Install the node-cron npm package 

    npm i node-cron

    Step 3: Create an index.js file in your root directory and import node-cron 

    import cron from 'node-cron'
    const job = cron.schedule('* * * * *', () => {
     console.log('Cron job now triggered');
    });

    Running node index.js should result in the log above appearing in the console every minutes.

    Format of a cron schedule string

    The cron schedule is made up of 6 time fields, but the second field is optional and is hardly used.
    # ┌────────────── second (optional)

    # │ ┌──────────── minute

    # │ │ ┌────────── hour

    # │ │ │ ┌──────── day of month

    # │ │ │ │ ┌────── month

    # │ │ │ │ │ ┌──── day of week

    # │ │ │ │ │ │

    # │ │ │ │ │ │

    # * * * * * *

    Create a function to perform any scheduled calculation or task 

    function sum(num1, num2) {
       const total = num1 + num2
       console.log (total)
    }

    Examples 

    1. Runs every minute between 3:00 PM and 3:59 PM every day and triggers the sumNumber function:
    const job = cron.schedule('* 15 * * *', () => {
    
     console.log('cron job now triggered');
    
     sum(4, 8)
    
    });
    
    job.start()

    2. Runs every minute and triggers the sumNumber function:

    const job = cron.schedule('* * * * *', () => {
    
     console.log('cron job now triggered');
    
     sum(4, 8)
    
    });
    
    job.start()

    3. Runs every 10 minutes from 9 AM to 5 PM, Monday to Friday and triggers the sumNumber function:

    const job = cron.schedule('*/10 9-17 * * 1-5', () => {
    
     console.log('cron job now triggered');
    
     sum(4, 8)
    
    });
    
    job.start()

    4. Runs by 12:30pm everyday and triggers the sumNumber function:

    const job = cron.schedule('30 12 * * *', () => {
    
     console.log('cron job now triggered');
    
     sum(4, 8)
    
    });
    
    job.start()

    Pros of setting up cron job with node-cron

    • Very easy to set up.
    • Makes use of familiar cron syntax.
    • Runs within your Node.js application.
    • Easy to test and debug within your development environment.

    Cons of setting up cron job with node-cron

    • Job runs only when your application is running so if your application fails, the job stops.
    • Keeping the process running continuously to handle scheduled tasks consumes memory and system resources.
    • Missed or failed jobs will not retry unless you implement retries yourself.

    Conclusion

    The node-cron scheduler is an excellent choice for simple, lightweight task scheduling directly within your Node.js applications. It leverages familiar cron syntax and integrates seamlessly with your codebase, making it easy to implement, test, and maintain. However, for critical or high availability tasks, considering that node-cron depends on your app’s uptime and lacks built in retry mechanisms. For such cases, dedicated job schedulers or external cron services may be more appropriate.

    Thank you for reading !!!

    Share. Facebook Twitter LinkedIn Email Copy Link
    Kaosarat Tolani Badmus
    • Website

    Add A Comment
    Leave A Reply Cancel Reply

    Latest Posts

    Oculus Quest X Headset: Discover a Shining New Star

    January 5, 2021

    iPhone Pro 13 Rumored to Feature 1 TB of Storage

    January 5, 2021

    Fujifilm’s 102-Megapixel Camera is the Size of a Typical DSLR

    January 5, 2021

    Apple Watch review: price drop makes Apple’s smartwatch more affordable

    January 30, 2016
    Artificial Intelligence
    The BridgeTechAcademy Blog
    Facebook X (Twitter) Instagram YouTube LinkedIn
    • Courses
    • Tech
    • Gadgets
    • Buy Now
    © 2026 BridgeTechAcademy

    Type above and press Enter to search. Press Esc to cancel.