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.

  • crontab -e: edits the current user's crontab file. This is the recommended method for application crons.
  • crontab -l: lists the current user's crons. Use sudo crontab -l to see root crons.
  • /etc/crontab: global system crontab file. Contains an additional user field.
  • /etc/cron.d/: directory for crons installed by packages. Each file is an independent crontab.

Common Linux issues

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

  • Cleared crontab: a system update or accidental crontab -r can delete all your crons. Regularly backup with crontab -l > backup.txt.
  • Environment variables: cron runs with a minimal environment (limited PATH, no shell variables). Explicitly define PATH and source your environment files.
  • Permissions: the script or its directories don't have proper permissions. Check with ls -la and chmod +x.
  • Unredirected outputs: by default, outputs are sent via local mail. Without a configured MTA, they are lost. Always redirect with >> /var/log/mycron.log 2>&1.

Heartbeat monitoring integration

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

  • End of line addition: add && curl -fsS https://api.monitao.com/ping/TOKEN at the end of each cron line. The && ensures the ping is only sent on success.
  • Wrapper script: create a script that wraps your command and handles the heartbeat. More flexible for error handling and metrics.
  • Integrated logging: combine local logging and heartbeat to have both execution history and real-time alerts.
  • Adapted timeout: configure the MoniTao timeout based on cron frequency. For an hourly cron, a 90-minute timeout is reasonable.

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.

  • Check system logs: journalctl -u cron on systemd, or grep CRON /var/log/syslog to see if the cron was invoked.
  • Simulate cron environment: test with env -i /bin/bash -c "your_command" to reproduce cron's minimal environment.
  • Capture all errors: redirect stderr to stdout with 2>&1 and log to a file to see error messages.
  • Add debug logs: insert echo "$(date) - Script started" >> /tmp/debug.log at script start to confirm execution.

Alert configuration

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

  • Cron not executed: no ping received within configured delay. Check the cron service, crontab, and permissions.
  • Explicit failure: the script sent a /fail ping. Check logs to identify the cause.
  • Abnormal duration: the cron takes longer than usual. May indicate a performance or resource problem.
  • Multiple failures: several consecutive executions have failed. Requires urgent investigation.

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.