How to Use Uptime Kuma Push Monitors to Track Cron Job Success
Learn how to monitor background tasks and cron jobs using Uptime Kuma push monitors to ensure your automated scripts are running as expected.
Introduction
Monitoring background tasks is a common challenge in systems administration and software engineering. While HTTP-based services are easily checked via standard probes, background scripts, cron jobs, and batch processes do not expose an endpoint for a monitoring tool to query. This creates a visibility gap: a job might fail silently, leaving your database out of sync or your reports ungenerated, without triggering any alerts.
Uptime Kuma provides a powerful solution for this through its "Push" monitor type. Unlike traditional monitors that actively poll a target, a push monitor acts as a passive receiver. Your script or cron job sends a "heartbeat" signal to Uptime Kuma upon successful completion. If the signal fails to arrive within a specified window, Uptime Kuma marks the monitor as down and triggers your configured alerts. This guide explores how to implement this pattern to ensure your automated workflows remain reliable.
Understanding the Push Monitor Concept
At its core, a push monitor is a unique URL generated by Uptime Kuma that expects a GET or POST request. When you create a push monitor, Uptime Kuma assigns it a specific URL containing a unique token. Your task is to ensure that your script hits this URL only when the job finishes successfully.
This approach is fundamentally different from polling. In a polling setup, the monitor controls the frequency of checks. In a push setup, the job controls the frequency. This is ideal for cron jobs that run on irregular schedules or tasks that take varying amounts of time to complete. If you are looking for a reliable environment to host your monitoring stack, you might consider [setup-uptime-kuma-docker-compose] to get started.
Setting Up a Push Monitor in Uptime Kuma
Before you can track your jobs, you must configure the monitor in the Uptime Kuma dashboard.
- Log in to your Uptime Kuma instance.
- Click "Add New Monitor".
- Set the Monitor Type to "Push".
- Provide a descriptive name, such as "Daily Database Backup Job".
- Set the "Heartbeat Interval". This is the maximum time you expect to pass between successful job runs. If your job runs every 24 hours, set this to 25 or 26 hours to account for minor execution delays.
- Click "Save".
Once saved, Uptime Kuma will display a unique URL. Copy this URL; you will need it for your script. This URL is the only communication channel between your task and the monitor.
Integrating the Push Monitor into Your Cron Jobs
To make your cron jobs report their status, you need to append a command to your existing script that triggers the push URL. The simplest way to do this is using curl or wget.
Using curl
Assuming your push URL is https://kuma.example.com/api/push/your-unique-token?status=up&msg=OK&ping=, you can add the following line to the end of your bash script:
#!/bin/bash
# Your existing backup logic here
pg_dump my_database > /backups/db.sql
# Notify Uptime Kuma of success
curl -fsS -m 10 --retry 5 -o /dev/null "https://kuma.example.com/api/push/your-unique-token?status=up&msg=Backup+Successful"
Handling Failures
It is equally important to report failures. If your script encounters an error, you can send a "down" status to Uptime Kuma, which will trigger an immediate alert rather than waiting for the heartbeat timeout.
#!/bin/bash
# Run the job and capture the exit code
./run_my_task.sh
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
curl -fsS "https://kuma.example.com/api/push/your-unique-token?status=up&msg=Success"
else
curl -fsS "https://kuma.example.com/api/push/your-unique-token?status=down&msg=Failed+with+code+$EXIT_CODE"
fi
Deep Dive: Why Push Monitoring Matters
Traditional monitoring systems rely on centralized polling. A monitoring agent reaches out to a target, checks its health, and records the result. This works perfectly for web servers, databases, or API endpoints. However, cron jobs are transient by nature. They exist only for the duration of their execution. If a monitoring agent tries to poll a cron job that isn't running, it will always receive a connection error. If it tries to poll when the job is running, it might catch the job in an intermediate state, leading to false positives.
Push monitoring inverts this control flow. The job itself becomes an active participant in the monitoring ecosystem. This is a form of observability that treats the job's lifecycle as a series of events. By sending a signal at the end of the execution, the script confirms that it reached the desired state. This is particularly useful for tasks that perform complex data transformations or network-heavy operations where failure is common.
Best Practices for Reliable Monitoring
1. Account for Execution Time
Always set your Uptime Kuma heartbeat interval to be slightly longer than your expected cron schedule. If a job runs once a day, setting the interval to exactly 24 hours is risky because minor clock drifts or system load can cause the job to start a few seconds late, triggering a false positive alert. A 5-10% buffer is usually sufficient.
2. Use Unique Tokens
Never reuse the same push URL for different jobs. Each task should have its own dedicated push monitor in Uptime Kuma. This ensures that when an alert fires, you know exactly which job failed without needing to parse generic logs. Using unique tokens also simplifies the process of configuring [configuring-uptime-kuma-notifications-webhooks] for specific tasks.
3. Secure Your Push URLs
While the tokens are unique, they are still public URLs. If your Uptime Kuma instance is exposed to the internet, ensure you are using HTTPS. If you are concerned about security, you can restrict access to the push endpoint via your reverse proxy (e.g., Nginx or Traefik) by allowing only specific IP addresses or requiring a client certificate.
4. Idempotency
Ensure your scripts are idempotent. If a job fails and you manually re-run it, the script should be able to handle the state correctly. For more on this, see our guide on [idempotent-scheduled-jobs-automation]. An idempotent script ensures that re-running a failed job doesn't result in duplicate data or corrupted states.
Advanced Monitoring Patterns
For more complex workflows, you might want to chain jobs. If Job B depends on Job A, you can use [advanced-uptime-kuma-alerting-dependency-chains] to ensure that alerts are suppressed for Job B if Job A has already failed. This prevents alert fatigue by focusing on the root cause of the failure.
Additionally, if you are running these jobs within Docker containers, you might want to integrate your monitoring with [debugging-docker-container-exits] to catch issues where the container itself crashes before the script can even report its status. Combining container health checks with application-level push monitors provides a robust, multi-layered defense against silent failures.
Diagnosing Common Issues
If your monitor is not showing "up" status despite the job running, check the following:
- Network Connectivity: Does the server running the cron job have access to the Uptime Kuma instance? Test with
curl -v <push-url>from the server. - Firewall Rules: Ensure that the server running the cron job is not blocked by a firewall from reaching the Uptime Kuma host.
- Token Validity: Verify that the URL in your script matches exactly what is displayed in the Uptime Kuma dashboard. Even a single missing character will prevent the signal from being registered.
- Log Inspection: Check your script's logs to see if the
curlcommand itself is failing. Using the-vflag withcurlcan provide verbose output to help debug connection issues.
Architectural Considerations
When scaling your monitoring infrastructure, consider the impact of push monitors on your Uptime Kuma instance. While push monitors are lightweight, they still require database writes for every heartbeat. If you have thousands of cron jobs firing heartbeats every minute, you might notice increased I/O pressure on the underlying database. For large-scale deployments, ensure your [postgresql-connection-pooling-serverless] or local database configuration is tuned for high-frequency writes.
Furthermore, consider the network topology. If your cron jobs are running in a private VPC or a restricted network, ensure that the Uptime Kuma instance is accessible via a secure tunnel or VPN. Using a public URL for push monitoring is convenient but requires careful management of the endpoint to prevent unauthorized spamming of your monitoring system.
Reliability and Failover
What happens if the Uptime Kuma instance itself goes down? Your push monitors will stop receiving heartbeats, and all your jobs will eventually report as "down". This is a classic "who monitors the monitor" problem. To mitigate this, consider running a redundant Uptime Kuma instance or using a cloud-based health check service that can monitor your primary Uptime Kuma deployment.
Reliability is not just about the monitor; it is about the entire chain of execution. If your script fails to send the heartbeat because the server ran out of memory, you need to be able to detect that. This is where [measuring-real-memory-pressure-linux] becomes relevant. By monitoring the host system's health alongside the job's success, you gain a complete picture of why a job might have failed.
Conclusion
Using Uptime Kuma push monitors is a lightweight and effective way to gain visibility into background tasks. By shifting from a polling model to a push model, you ensure that your monitoring system is aware of the actual completion status of your scripts. This simple integration pattern helps maintain the integrity of your automated infrastructure and ensures that you are the first to know when a critical task fails. Remember to keep your heartbeat intervals generous, your tokens secure, and your scripts idempotent to build a truly resilient monitoring setup. By combining these practices, you can transform your background tasks from "black boxes" into observable, reliable components of your system.
Frequently Asked Questions
What happens if my cron job takes longer than the heartbeat interval?
If the job takes longer than the interval, Uptime Kuma will mark the monitor as down because it has not received a new signal within the expected timeframe. You should always set your heartbeat interval to be longer than the maximum expected execution time of your longest-running job to avoid false alerts.
Can I send custom error messages to Uptime Kuma?
Yes, you can append the msg parameter to your push URL to send custom text. This is useful for including error codes or brief descriptions of why a job failed, which will then appear in your Uptime Kuma dashboard and alert notifications.
Is it possible to use push monitors for services that are not cron jobs?
Yes, you can use push monitors for any process that can execute an HTTP request. This includes long-running background workers, event-driven scripts, or even manual tasks where you want to confirm that a human has completed a specific action.
How do I verify that my push monitor is working correctly?
You can verify the setup by manually running the curl command from your terminal on the server where the job resides. If the command executes successfully and you see the status change to "up" in the Uptime Kuma dashboard, your integration is working as expected.