In this lesson, you will learn how to systematically parse system logs to identify why your scheduled background tasks are failing.
When a cron job fails silently, the cause is often hidden in the system's log streams. Since cron is a system service, its execution history is recorded by either the legacy syslog daemon or the modern journald.
Locating Cron Logs
On many systems, cron activities are sent to /var/log/syslog or /var/log/cron. If you are on a system using systemd, the most reliable way to view these logs is through journalctl. You can filter for the cron service specifically:
journalctl -u cron --since "1 hour ago"
Interpreting the Trace
When you view the logs, look for the standard execution pattern:
- The Trigger: A log entry confirming the cron daemon invoked your command.
- The User Context: The log will typically show which user the task ran as (e.g.,
(root) CMD (...)). - The Result: If the command output errors, cron often captures
stderrand attempts to send it via local mail, but the log will confirm if the process returned a non-zero exit status.
For example, you might see:
CRON[1234]: (user) CMD (/usr/local/bin/backup.sh)
CRON[1234]: (CRON) info (No MTA installed, discarding output)
The second line is a critical hint. It tells you that your script produced output (or errors) that cron tried to email to you, but failed because no mail server (MTA) was configured. To debug this, you should redirect your script's output to a file or log it explicitly:
# In your crontab, redirect both stdout and stderr to a log file
* * * * * /usr/local/bin/backup.sh >> /var/log/myjob.log 2>&1
Troubleshooting Tips
- Environment Mismatch: If your log shows
command not found, remember that cron runs with a minimalPATH. Always use absolute paths for your binaries (e.g.,/usr/bin/python3instead of justpython3). - Permissions: Check the logs to see if the command was rejected due to permission issues, which often appear as
Access deniedorPermission deniederrors in the journal.
Key Takeaway: Use journalctl -u cron to verify execution flow, and always redirect output in your crontab to capture diagnostic data that the system mailer might otherwise discard.