How to Monitor and Troubleshoot Cron Jobs in Ubuntu: Checking Logs and Service Status


2 views


Unlike some other Linux distributions, Ubuntu doesn't create a dedicated /var/log/cron file by default. The cron service logs through rsyslog, and you'll typically find cron-related messages in the main system log:

sudo grep CRON /var/log/syslog

First verify that the cron service is actually running:

# For systems using systemd systemctl status cron # Older systems using init service cron status

You should see output indicating the service is active (running). If not, start it with:

sudo systemctl start cron

To enable more detailed logging, edit the rsyslog configuration:

sudo nano /etc/rsyslog.d/50-default.conf

Uncomment or add this line:

cron.* /var/log/cron.log

Then restart the services:

sudo systemctl restart rsyslog sudo systemctl restart cron

For individual cron jobs, you can redirect output to log files in the crontab itself:

* * * * * /path/to/script.sh >> /var/log/my_cron.log 2>&1

Or use logger to send messages to syslog:

* * * * * /path/to/script.sh 2>&1 | logger -t my_cron_job

1. Check for environment differences - cron runs with a minimal environment:

* * * * * env > /tmp/cron_env.log

2. Verify permissions on scripts and log files

3. Test commands manually using the same user context:

sudo -u [username] [command]

4. For mail-related issues (cron sends output via mail by default):

sudo apt install postfix

For security-conscious environments, configure auditd to track cron execution:

sudo apt install auditd sudo auditctl -w /usr/sbin/cron -p x -k cron_execution sudo ausearch -k cron_execution | aureport -f -i

Unlike some other Linux distributions, Ubuntu uses syslog for cron logging by default. The main locations to check are:

/var/log/syslog
/var/log/cron.log  # If rsyslog is configured for cron

First check if the cron daemon is active:

sudo systemctl status cron

You should see output like:

● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-05-10 09:25:33 UTC; 1h ago

If you're not seeing cron logs, you may need to enable them in rsyslog:

sudo nano /etc/rsyslog.d/50-default.conf

Uncomment or add this line:

cron.*  /var/log/cron.log

Then restart the services:

sudo systemctl restart rsyslog
sudo systemctl restart cron

To verify your crontab entries:

crontab -l

For system-wide cron jobs, check:

ls /etc/cron.*/
cat /etc/crontab

Add logging to your cron jobs for better debugging:

* * * * * /path/to/script.sh >> /var/log/my-cron.log 2>&1

Or redirect output to syslog:

* * * * * /path/to/script.sh | logger -t mycron