Monitor Docker Cron Containers

Learn how to monitor scheduled tasks running in Docker containers and Kubernetes CronJobs.

Features

  • Works with Docker Compose and Kubernetes
  • Health check endpoint integration
  • Sidecar container pattern
  • Init container monitoring

Code Examples

Dockerfile
FROM alpine:latest

RUN apk add --no-cache curl bash

COPY backup.sh /app/backup.sh
RUN chmod +x /app/backup.sh

# Wrapper script with monitoring
COPY <<'EOF' /app/run.sh
#!/bin/bash
CHECK_URL="https://haspulse.io/ping/$CHECK_ID"
curl -fsS "$CHECK_URL/start"
/app/backup.sh
if [ $? -eq 0 ]; then
    curl -fsS "$CHECK_URL"
else
    curl -fsS "$CHECK_URL/fail"
fi
EOF
RUN chmod +x /app/run.sh

CMD ["/app/run.sh"]
docker-compose.yml
version: '3.8'
services:
  backup:
    build: .
    environment:
      - CHECK_ID=YOUR_CHECK_ID
    # Run daily at 3 AM using ofelia or supercronic
    labels:
      ofelia.enabled: "true"
      ofelia.job-exec.backup.schedule: "0 3 * * *"
      ofelia.job-exec.backup.command: "/app/run.sh"
Kubernetes CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
  name: database-backup
spec:
  schedule: "0 3 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: backup
            image: your-backup-image
            env:
            - name: CHECK_ID
              valueFrom:
                secretKeyRef:
                  name: haspulse
                  key: check-id
            command:
            - /bin/sh
            - -c
            - |
              curl -fsS "https://haspulse.io/ping/$CHECK_ID/start"
              /app/backup.sh && \
                curl -fsS "https://haspulse.io/ping/$CHECK_ID" || \
                curl -fsS "https://haspulse.io/ping/$CHECK_ID/fail"
          restartPolicy: OnFailure

How It Works

  1. 1
    Create a check in Haspulse
    Set your expected schedule (e.g., every 5 minutes)
  2. 2
    Add the ping to your code
    Ping the unique URL when your job runs successfully
  3. 3
    Get alerted on failures
    If a ping doesn't arrive on time, we notify you

Related Integrations

Start monitoring in minutes

Create your first check and start receiving alerts when your scheduled jobs fail.

Get started free