Linux Crontab Monitoring

Master monitoring of your Linux scheduled tasks

Cron is the historical and most widely used scheduling tool on Linux. For over 40 years, it has enabled automating script execution at defined times. Despite its apparent simplicity, cron hides many pitfalls that can lead to silent failures, going unnoticed for days or even weeks.

Common issues include crontabs cleared during system updates, missing environment variables, relative paths that don't work, and scheduling syntax errors. Without external monitoring, these problems remain invisible because cron itself has no built-in alerting mechanism.

MoniTao offers a heartbeat monitoring solution that perfectly complements Linux cron. By adding a simple curl call at the end of your scripts, you are immediately alerted if a cron doesn't execute or fails, whatever the reason.

Essential crontab reminders

Before setting up monitoring, here are the key commands and files for cron management on Linux.

Common Linux issues

These issues are responsible for the majority of cron failures on Linux. Knowing these pitfalls helps avoid them.

Heartbeat monitoring integration

Here are strategies to integrate MoniTao with your existing Linux crons.

Crontab integration examples

Here are several methods to integrate heartbeat monitoring into your Linux crons:

# Method 1: Direct addition in crontab
0 * * * * /path/to/backup.sh && curl -fsS "https://api.monitao.com/ping/TOKEN"

# Method 2: Wrapper script with error handling
#!/bin/bash
set -e
/path/to/backup.sh
curl -fsS --max-time 10 "https://api.monitao.com/ping/TOKEN"

# Method 3: With logging and failure notification
#!/bin/bash
LOG=/var/log/backup.log
if /path/to/backup.sh >> $LOG 2>&1; then
    curl -fsS "https://api.monitao.com/ping/TOKEN"
else
    curl -fsS "https://api.monitao.com/ping/TOKEN/fail?msg=Backup+failed"
fi

Method 1 is the simplest for existing crons. Method 2 offers better error handling with set -e. Method 3 allows explicitly notifying failures with an error message.

Debugging Linux crons

When a cron doesn't work, here are the systematic diagnostic steps.

Alert configuration

Configure your MoniTao alerts to be notified of problems without being overwhelmed.

Linux cron checklist

  • crontab -l executed and verified for each user
  • All paths are absolute in scripts
  • Outputs redirected to log files
  • MoniTao heartbeat added to each critical cron
  • Manual script test in clean environment
  • Crontab backup performed regularly

Frequently asked questions about Linux cron

My cron works from command line but not via crontab. Why?

The cron environment is minimal: reduced PATH, no custom shell variables. Use absolute paths (e.g., /usr/bin/php instead of php) and explicitly source your environment files if necessary.

How can I see my cron execution logs?

On systemd: journalctl -u cron. On classic systems: /var/log/cron or grep CRON /var/log/syslog. For detailed logs, redirect your script outputs to a dedicated file.

My cron runs twice simultaneously. Why?

Check that the cron isn't defined in multiple places: user crontab (crontab -l), system file (/etc/crontab), and /etc/cron.d/ directory. Remove duplicates.

How do I prevent two concurrent executions of the same cron?

Use flock to create a lock: flock -n /tmp/myjob.lock ./myjob.sh. If the lock is already taken, the new instance stops immediately.

My crontab disappeared after an update. How do I prevent this?

Regularly backup your crontabs with crontab -l > /backup/crontab-$(whoami).txt. Automate this backup with... a cron! Also store a copy in your configuration manager.

How do I test a cron without waiting for its scheduled time?

Execute manually in a cron-like environment: env -i HOME=$HOME /bin/bash -c "your_script.sh". This simulates the minimal environment and reveals PATH or variable issues.

Conclusion

Linux cron remains the most widely used scheduling tool, but its simplicity masks many pitfalls. Silent errors, lost crontabs, and environment issues are frequent causes of undetected failures.

By combining cron configuration best practices with MoniTao heartbeat monitoring, you gain complete visibility into your scheduled task execution. Start with your critical crons and gradually extend coverage.

Ready to Sleep Soundly?

Start free, no credit card required.