Where are Crontab Logs in Linux | How to View Them?
If you are a Linux administrator or trying to become one, you must gather strong knowledge related to cron jobs and cron logs. The Linux System Administrator is responsible for running the scheduled tasks (known as jobs) and ensuring they work perfectly fine by monitoring their results. This verification of successful job execution is done by the Crontab logs, which are automatically created by the system.
This post will act as a guide to show you all you need to know about cron logs that will help you become a Linux System administrator.
What are Crontab Logs in Linux?
Crontab logs are the records generated by the cron daemon that provides information about the execution of cron jobs. These logs are stored in the system log files.
The logs contain information about when the cron jobs were run, which commands were executed, and any output generated by the commands. The logs also record any errors or warnings during the execution of the cron jobs.
By monitoring the crontab logs, system administrators can troubleshoot issues related to cron job execution and ensure that the scheduled tasks are running as intended. Additionally, the logs can be used to audit system activity and ensure compliance with security policies.
Where are the Crontab Logs Located in Linux?
The cron logs are placed inside the system logs file, which is in the directory “/var/log.” However, the path of the file depends on the Linux distribution you are using.
Debian/Ubuntu-Based Distributions:
/var/log/syslog
CentOS/RHEL and Fedora-Based Linux Distributions:
/var/log/cron
How to View Crontab Logs in Linux?
As discussed, it is a file that shows the activity logs of the system. The crontab logs can be viewed using various Linux utilities i.e., cat, tail, and head, on the default locations as discussed above. Apart from viewing the logs directly from the default files, the rsyslog server can also be utilized to view the cron logs.
Approach 1: View Cron Logs Through the Default Locations
Let’s use the head, tail, and cat utilities to view the crontab logs:
View All the Crontab Logs
Using the cat utility without any flags will list all the logs in a file:
For Debian/Ubuntu:
cat /var/log/syslog
For CentOS/RHEL:
cat /var/log/cron
The output of this file is quite messy. Let’s break it down for you:
- The date and time that the cron job was executed.
- The user account that executed the cron job.
- The command that was executed as part of the cron job.
- The output or error messages generated by the command during execution.
- The information about the exit status of the command, including whether it succeeded or failed.
Any other relevant information about the execution of the cron job, such as the resources it used or any other system events that occurred at the time of execution.
View Only Recent Crontab Logs
The tail command lists the last ten lines of a file. So, if we talk about the cron logs, the recent logs are at the end of the file which can be viewed using the tail command as follows:
For Debian/Ubuntu:
tail /var/log/syslog
For CentOS/RHEL:
tail /var/log/cron
Apart from the basic usage of the tail command, it supports the “-n” flag to mention the number of lines to view.
View the Oldest Cron Logs
Like the tail utility, the head command prints the first ten lines of a file on the terminal. So, the head utility can be used to extract only the earliest logs from the files.
For Debian/Ubuntu:
head /var/log/syslog
For CentOS/RHEL:
head /var/log/cron
Approach # 2: View Crontab Logs Through the cron.log File
The cron.log file is associated with the rsyslog server. This is used by the rsyslog to get the crontab info when required. The rsyslog may not be installed on your Linux distribution. So, before using this, you need to install the rsys server and start it on your system. So, let’s start with the installation.
Note: This method is limited to Debian/Ubuntu-based distributions. In order to do it on other Linux distributions, you have to perform all this manually, creating the 50-default config file in rsyslog.d
Step 1: Install rsyslog on Debian/Ubuntu using the command:
sudo apt install rsyslog
Once it is installed, you need to start the rsys services on your system:
sudo systemctl start rsyslog
Step 2: Create a cron.log file inside the directory “/var/log/” using the command:
sudo touch /var/log/cron.log
Step 3: Now, open the “50-default.conf” file and uncomment the line starting with the “cron.*”:
nano /etc/rsyslog.d/50-default.conf
Step 4: After doing so, you need to restart rsys services:
sudo systemctl restart rsyslog
Now, use any of the Linux command (cat, tail, head) to get the content of corn:
cat /var/log/cron.log
Dracula VPS Hosting Service
Dracula Servers offers high-performance server hosting at entry-level prices. The plans include Linux VPS, Sneaker Servers, Dedicated Servers & turnkey solutions. If you’re looking for quality self-managed servers with high amounts of RAM and storage, look no further.
Check the plans for yourself by clicking Here!
How to Watch Crontab Logs in Real-Time?
The Cron Logs can be viewed in a real-time environment. For this, a script can be created that will monitor the cron logs through the watch cron utility. Let’s understand it via the following steps:
Step 1: Create a realtimecron named file using the touch command (or the nano editor can also be used):
sudo nano realtimecron
Step 2: Add the following watch command to look for changes in the file. Here, the first “n” option is used to define the number of seconds. While the tail utility will only list the last few entries of the file “/var/log/cron.log”:
watch -n 5 tail -n 10 /var/log/cron.log
Step 3: Now, use the chmod command to make the script executable:
sudo chmod +x realtimecron
Step 4: Now, make a copy of this file in the directory, i..e, /usr/sbin:
sudo cp watchcron /usr/sbin
That’s all. Now, you can execute the file to look for the real-time cron logs. Remember that it will only list the number of entities defined using the watch command:
realtimecron
Wrap Up
Understanding cron jobs and cron logs is essential for Linux administrators or anyone aspiring to become one. The cron logs provide valuable information about the execution of scheduled tasks, including commands executed, output generated, errors or warnings, and the success or failure of the jobs.
These logs are stored in system log files, which are located in the /var/log directory, with the path varying depending on the Linux distribution used.