Monitor cronjobs in Node.js

Learn to monitor cronjobs in Node.js using Operational. Track job runs, failures, and durations in real time.

What is cronjob monitoring in Node.js

If you’re using a package like croner, monitoring cronjobs in Node.js helps you catch failures and track performance of scheduled tasks. For example, you might send daily reports to yourself or clean up data every hour.

If a job fails silently, you lose insights and might miss critical alerts.

Using Operational to Track Cronjobs

Operational is an open-source event tracking tool for tech products. You can easily track cronjob events like start, success, and failure. This helps you spot issues and analyze job runs over time.

Operational in action

Setting Up Operational

  1. Go to https://app.operational.co and sign up for an account.
  2. Create a new project and get your API key from project settings.
  3. Install the SDK in your Node.js app:
    npm install @operational.co/sdk
  4. Save your API key as an environment variable:
    export OPERATIONAL_API_KEY="YOUR_API_KEY"

Example Code

The code below shows how to send events when a cronjob starts, succeeds, or fails.

Paste this code examples inside your cronjob functions.

import Operational from "@operational.co/sdk";
const ops = new Operational(process.env.OPERATIONAL_API_KEY);

// Track job start
await ops.events.ingest({
  name: "cronjob started",
  avatar: "⏱️",
  content: "Daily report job started by system"
});

// Track job success
await ops.events.ingest({
  name: "cronjob successful",
  avatar: "",
  content: "Daily report job completed in 120 seconds"
});

// Track job failure
await ops.events.ingest({
  name: "cronjob failed",
  avatar: "",
  content: "Daily report job failed due to timeout"
});

This example shows how to log different stages of a cronjob.

Conclusion

Operational can save you time by collecting cronjob events in one dashboard. You avoid manual logging and get real-time alerts for failures.

Learn more at https://operational.co and try the playground at https://operational.co/playground.