Setting Up Cronjob Monitoring with Heartbeats
Learn how to monitor your Linux cronjobs with heartbeats. Complete guide covering cronjob setup, heartbeat integration, and monitoring best practices.
Cronjobs are essential for automating tasks on Linux systems, but knowing whether they’re running successfully can be challenging. upagent’s heartbeat monitoring provides a simple way to track your cronjobs and get alerted when they fail or don’t run as expected.
What Are Cronjobs?
Cronjobs are scheduled tasks that run automatically on Linux systems at specified intervals. They’re perfect for:
- Database backups
- Log file cleanup
- System maintenance scripts
- Data synchronization tasks
- Automated reports
Setting Up Cronjobs in Linux
1. Access the Crontab
Open your crontab editor to create or modify scheduled tasks:
crontab -e
2. Crontab Syntax
Cronjobs follow this format:
* * * * * /path/to/command
│ │ │ │ │
│ │ │ │ └─── Day of week (0-7, Sunday = 0 or 7)
│ │ │ └───── Month (1-12)
│ │ └─────── Day of month (1-31)
│ └───────── Hour (0-23)
└─────────── Minute (0-59)
3. Common Cronjob Examples
# Run every 5 minutes
*/5 * * * * /home/user/backup-script.sh
# Run daily at 2:30 AM
30 2 * * * /home/user/daily-cleanup.sh
# Run weekly on Sunday at midnight
0 0 * * 0 /home/user/weekly-report.sh
# Run monthly on the 1st at 3:00 AM
0 3 1 * * /home/user/monthly-maintenance.sh
Integrating upagent Heartbeats
1. Create a Heartbeat in upagent
- Log into your upagent dashboard at app.upagent.io
- Navigate to “Heartbeats” in the sidebar
- Click “Create Heartbeat”
- Set your cron schedule (e.g.,
*/5 * * * *
for every 5 minutes) - Set a grace period (e.g., “1 minute” for a 1 minute grace period, which means that the cronjob will be considered late if it runs more than 1 minute after the scheduled time)
- Copy the heartbeat URL - it will look like
https://api.upagent.io/beat/your-heartbeat-id
2. Add Heartbeat to Your Cronjobs
Modify your cronjobs to include a curl request to your heartbeat URL. Here are several approaches:
Simple Success Notification
Add the heartbeat call after your command:
# Run backup and notify upagent on success
*/5 * * * * /home/user/backup-script.sh && curl -s https://api.upagent.io/beat/your-heartbeat-id
3. Set Up Notifications
In your upagent dashboard:
- Configure notification integrations (Email, Slack, Discord, etc.)
- Configure your heartbeats to send notifications to the configured integrations
Best Practices
1. Use Descriptive Heartbeat Names
Name your heartbeats clearly to identify which cronjob they monitor:
- “Database Backup - Every 6 Hours”
- “Log Cleanup - Daily”
- “System Update Check - Weekly”
2. Set Appropriate Grace Periods
Allow some buffer time for your jobs to complete:
- Quick scripts: 1-2 minutes grace period
- Database operations: 10-30 minutes
- Large backups: 1-2 hours
3. Test Your Setup
Before deploying to production:
- Test your cronjob manually
- Verify the heartbeat is received in upagent
- Test failure scenarios to ensure alerts work
Troubleshooting Common Issues
Cronjob Not Running
# Check if cron service is running
systemctl status cron
# View cron logs
tail -f /var/log/cron
# List current cronjobs
crontab -l
Heartbeats Not Received
- Verify network connectivity:
curl -I https://api.upagent.io
- Check if curl is installed:
which curl
- Review cronjob output: redirect to a log file for debugging
Silent Failures
Use the 2>&1
redirect to capture both stdout and stderr:
*/5 * * * * /home/user/script.sh >> /var/log/cronjob.log 2>&1 && curl -s https://api.upagent.io/beat/your-heartbeat-id
Conclusion
Combining Linux cronjobs with upagent’s heartbeats provides robust monitoring for your automated tasks. This setup ensures you’ll know immediately when critical jobs fail or don’t run as expected, helping maintain system reliability and reducing downtime.
Start with simple heartbeat notifications and gradually add more sophisticated monitoring as needed. With proper setup, you’ll have complete visibility into your automated processes and peace of mind knowing your systems are running smoothly.