Debugging Anacron Failures: Diagnosing and Fixing Cron Job Execution Issues in Ubuntu


2 views

The core symptom here is that scheduled daily tasks (specifically log rotation) haven't been executing since October 2009, as evidenced by the stale timestamp in /var/spool/anacron/cron.daily. This suggests a complete breakdown in the anacron service rather than just a configuration issue.

First, check if the anacron service is actually running:

systemctl status anacron
# OR for older systems:
service anacron status

If it's not running, attempt to start it manually:

sudo systemctl start anacron
# Check logs immediately after:
journalctl -u anacron -n 50

Anacron relies on several critical files and directories:

  • /etc/anacrontab - Main configuration file
  • /var/spool/anacron/ - Timestamp directory
  • /etc/cron.*/ - Job directories

Verify permissions on these critical paths:

ls -ld /var/spool/anacron/ /etc/anacrontab /etc/cron.daily/
stat -c "%a %U:%G %n" /var/spool/anacron/

Trigger anacron manually in debug mode to see real-time output:

sudo anacron -T -d -n
# -T = test mode (no actual execution)
# -d = debug output
# -n = run jobs now (ignore timestamps)

For a full execution test (WARNING: will actually run jobs):

sudo anacron -d -n

Common issues that prevent anacron from running:

  1. Missing dependencies:
    ldd $(which anacron)
  2. Filesystem issues:
    df -h /var/spool/
  3. Permission problems:
    sudo -u nobody /usr/sbin/anacron -T

For a complete reset of the anacron environment:

# Stop service
sudo systemctl stop anacron

# Clear old timestamps
sudo rm -f /var/spool/anacron/*

# Verify configuration
sudo anacrontab -T

# Restart service
sudo systemctl start anacron

# Verify first run
sleep 60
journalctl -u anacron --since "1 minute ago"

Since sysklogd isn't using logrotate, implement direct logging:

# Create custom anacron log
sudo touch /var/log/anacron.log
sudo chown root:adm /var/log/anacron.log
sudo chmod 640 /var/log/anacron.log

# Add to /etc/rsyslog.d/anacron.conf
:programname, isequal, "anacron" /var/log/anacron.log
& stop

First, let's verify if anacron is actually running on your Ubuntu system. Run these commands in terminal:

# Check if anacron service is active
systemctl status anacron

# Alternative method using process list
ps aux | grep anacron

If the service isn't running, you'll need to start it manually:

sudo systemctl start anacron
sudo systemctl enable anacron  # To start at boot

The key locations to inspect are:

/etc/anacrontab         # Main configuration file
/var/spool/anacron/     # Timestamp directory
/etc/cron.daily/        # Daily cron scripts

Check permissions on these critical paths:

ls -la /var/spool/anacron/
ls -la /etc/cron.daily/

Force a test run of the daily jobs to see any errors:

sudo /usr/sbin/anacron -T -d -n

The flags mean:
-T: Test mode (won't update timestamps)
-d: Debug output
-n: Run jobs now (ignore delay periods)

For the specific log rotation problem, you might need to:

# Remove the stale timestamp file
sudo rm /var/spool/anacron/cron.daily

# Then manually run the daily jobs
sudo run-parts /etc/cron.daily/

To generate detailed logs for troubleshooting:

# Create a dedicated log file
sudo touch /var/log/anacron.log
sudo chmod 644 /var/log/anacron.log

# Edit the anacrontab to include logging
sudo nano /etc/anacrontab

Add this line at the top of the file:

LOGNAME=anacron
LOGFILE=/var/log/anacron.log

If anacron proves unreliable, consider using a systemd timer as a replacement:

# Create a systemd service file
sudo nano /etc/systemd/system/daily-jobs.service

[Unit]
Description=Run daily cron jobs

[Service]
Type=oneshot
ExecStart=/bin/run-parts /etc/cron.daily/

# Then create the timer
sudo nano /etc/systemd/system/daily-jobs.timer

[Unit]
Description=Run daily jobs timer

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Enable and start the timer:

sudo systemctl enable daily-jobs.timer
sudo systemctl start daily-jobs.timer