How to Check Crontab logs in Linux
As a Linux user, you are probably already familiar with crontab. You can automate tasks by running commands and scripts at a predefined schedule. Want to automatically take backups? Crontab is your friend.
I am not going into the usage of crontab here. My focus is on showing you the different ways to check crontab logs.
It helps investigate whether your cronjobs ran as scheduled or not.
Method 1: Check the syslog for crontab logs
As per the Linux directory hierarchy, the /var/log
directory in Linux stores logs from the system, services, and running applications.
While the cron logs are also in this directory, there is no standard file for these logs. Different distributions keep them in different files.
For Debian-based distributions, the file /var/log/syslog
contains the logs from the cron job execution and should be consulted in case your cron jobs are not working:
cat /var/log/syslog | grep -w 'cron’
You will see all cron jobs listed on your terminal when the above command is run. The grep command will filter the cron related messages apart from the rest.
For RedHat-based distros, the cron logs have dedicated file /var/log/cron
.
In both cases, you will probably need to specify the sudo
keyword or use the root account to access the logs.
Method 2: Use a custom log file (recommended)
Using a separate custom file for logging cron jobs is a recommended practice.
For this, you can configure ‘rsyslog’ to forward cron logs. Rsyslog is a Linux service that has features similar to Syslog logging.
Simply create a file cron.log
under the directory /etc/rsyslog.d
:
touch /var/log/cron.log
Now open the file /etc/rsyslog.d/50-default.conf
for editing:
nano /etc/rsyslog.d/50-default.conf
and locate the line starting with #cron.*
and remove the #
at the start of the line.
To make the changes work, save and close this file and finally restart the rsyslog service and check its status:
sudo systemctl restart rsyslog
sudo systemctl status rsyslog
The status of the service should be highlighted as active (running).
Now, whenever you need to access the crontab logs, just read the content of this log file:
less /var/log/cron.log
Method 3: Use dedicated services like Cronitor monitor cron jobs
Cronitor is a service that can be deployed to monitor any type of cron job.
Many of the cron versions will start logging when the scheduled job executes or if there are any problems with the crontab. However, the output from the cron job or its exit status is not logged.
Here Cronitor comes in handy and works perfectly. It is a complete solution for all your crontab needs. It captures logs, metrics, and status from all the jobs and creates instant alerts for crashed or failed to start cron jobs.
You can see all this through a web-based interface.
For Cronitor or CronitorCLI installed on Kubernetes, logs can be captured for as high as 100MB for a single execution. Here, you can find the detailed installation steps on Linux for CronitorCLI.
Other monitoring tools and services like Better Uptime also provide the feature to monitor the cron jobs automatically.
Conclusion
System log files are very crucial for troubleshooting and diagnosing system-related issues, cron logs are thus no exception for this.
The Syslog keeps the logs related to crontab however, having a dedicated log file for cron is recommended. A web-based service like Cronitor, Better Uptime, and Uptime Robot also helps.
By the way, did you know about the at command that can also be used for scheduling tasks?