Linux Commands

How to check cron logs in Linux

In a Linux environment, the most common word ‘cron jobs’ is mostly used. For those who don’t know about that. A cron job is a task scheduler that automates all repetitive tasks in a Linux distribution. Cron jobs are executed at a specified date and time, which is scheduled by the system administrator. So, cron jobs logs or history are maintained in a log file that helps the system administrator to verify that either the cron jobs are executed at a specified time or not.

We will discuss in this article how a user can view the cron logs files in a Linux environment. We have performed all tasks on the Ubuntu 20.04 system that will give you a better understanding of the cron logs.

Open the terminal by pressing the keyboard shortcut ‘Ctrl+Alt+t’. Now, using the following two different methods can easily access the cron log events:

Method 1: Check cron logs events through syslog

It is a very simple and easy way to check that cron log events are running on your system. Login as root user on the terminal and type the following command:

# cat /var/log/syslog | grep cron

The following cron logs events should display on the terminal:

Method 2: Monitor cron logs by setting up the cron.log file

The recommended way is to create a separate ‘cron.log’ file to monitor or check the cron logs events on your Linux system. For this purpose, access the  ‘/etc/rsyslog.d/50-default.conf’ file by running the below-given command:

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

Search the ‘ #cron.* /var/log/cron.log’ in this file and uncomment this line which is also shown in the following screenshot:

Now, create a ‘cron.log’ by using any source code or text editor.

$ sudo nano /var/log/cron.log

Restart the rsyslog service and then check the running status of this service on your system through the following command:

$ sudo systemctl restart rsyslog

$ sudo systemctl status rsyslog

The following output should print on the terminal window:

Now, all cron log events to be saved in the cron.log file.

To view, the real-time cron logs events using the ‘watchcron’ command. So, create a ‘watchcron’ file as follows:

$ sudo nano watchcron

Add the following lines in this file:

#!/bin/bash

watch -n 10 tail -n 25 /var/log/cron.log

Save this file in nano using ‘Ctrl+o’ and then press ‘Ctrl+x’ to exit from this environment.

Here, the above watchcron refreshes the logs event page after 10 seconds and displays the last 25 events on the page.

Set the executable permissions on this file by using the following command:

$ sudo chmod +x watchcron

Copy this file in ‘/usr/sbin’ location as follows:

$ sudo cp watchcron /usr/sbin

Now, to check the real-time cron logs events run the below-given command on the terminal:

$ watchcron

The following window will show on the terminal:

Conclusion

We have explained In this article how you can check or monitor cron logs events in real-time by using a single ‘watchcron’ command.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.