The cron is the software utility known as the cron job. The cron job is a time-based job scheduler in Unix-like operating systems for maintaining the software environments to run periodically at fixed times, days, dates, or intervals. In Linux, the “Crontab” file can be created, modified, deleted, and viewed by using the “Crontab” command.
This article aims to discuss the possible aspects of checking the cron logs in Linux. The guideline of this article is listed below:
- Method 1: Use the syslog File to Check the Cron Logs
- Method 2: Use the cron.log file to Check the Cron Logs
Let’s start discussing how you can check cron logs in Linux.
Method 1: Use the syslog File to Check the Cron Logs
The “syslog” daemon stands for “System Logging Protocol” which allows the user to send the log information in one centralized place and communicate with the system information. In this method, the “syslog” file is used to check the running cron log events of the current working system.
First, open the terminal by pressing the shortcut key “Ctrl+Alt+T”. Run the following command that contains the combination of “cat” and “grep” as shown below:
$ cat /var/log/syslog | grep cron
The output shows the currency running cron logs in the terminal.
As it is the simple and easiest method to check the cron logs events running in the current Ubuntu system
Note: It is recommended to log in as a root user before executing the above command.
Method 2: Use the cron.log File to Check the Cron Logs
Another recommended method to check the cron log events files is to create a separate “cron.log” file. To perform this task use the carried out the specified step-by-step instructions. So let’s start with the first step.
Step 1: Access the Default Cron Log File
In the first step, open the the “/etc/rsyslog.d/50-default.conf” file in “nano” the default text editor of Ubuntu(or the user choice):
$ sudo nano /etc/rsyslog.d/50-default.conf
Enter the currently logged-in user password to continue the process. Find the “#cron.* /var/log/cron.log” comment line in the opened file that is shown below:
Uncomment the above-highlighted line and save the file via “Ctrl+S”. After that, exit the nano editor from the terminal by pressing the “Ctrl+X” shortcut key.
Step 2: Create the New cron.log File
Create the new “cron.log” file in any of the text editors available in the entire Linux system. In this case, the “nano” default text editor is used to create this file:
$ sudo nano /var/log/cron.log
Close the nano editor and restart the “rsyslog” service by the following “systemctl” command:
$ sudo systemctl restart rsyslog
Check the “status” command for checking the “rsyslog” services status:
$ sudo systemctl status rsyslog
The “green indicator” present in the third line shows that the services are in an “active(running)”. Hence all the cron loft events are saved into the created “cron.log” file.
Step 3: Check the Real-time Cron Logs
Now check all the real-time cron logs events by utilizing the “watch” command. To do so, first, create the “watchcron” file:
$ sudo nano watchcron
Type the following lines into the newly created “watchcron” file. The first line is the “Shebang” which tells the bash shell to run the script. The second line defines the “watch” command and its parameters:
#!/bin/bash
watch -n 15 tail -n 30 /var/log/cron.log
The above command will refresh the cron logs after every 15 seconds and display after every 30 seconds on the page.
Step 4: Change Watchcron File Permissions
To run the “watchcron” file first make it executable. For this purpose run the “chmod” command to assign “execute” permissions to the “watchcron” file.
$ sudo chmod +x watchcron
The permission has been assigned to the “watchcron” file.
Step 5: Execute the “watchcron” File
Copy the “watchcron” file into the “/usr/sbin” place with the help of the “cp” command:
$ sudo cp watchcron /usr/sbin
The “watchcron” file is copied to the “/usr/sbin” file as shown in the screenshot:
$ ls -l /usr/sbin/watchcron
After that execute the “./watchron” file and check the real-time cron log events running in the entire Linux system:
$ ./watchcron
It’s all about checking the cron logs in Linux.
Conclusion
In Linux to check the cron logs use the “syslog File” by utilizing the “cat” and “grep” commands. It can also be checked by creating a new “cron.log” and the “watchcron” file. To check the real-time cron logs in the terminal use the “watch” command described in the “watchcron” file along with its parameters. This article provides a detailed view of all the recommended methods to check the cron logs in Linux.