How to Check Crontab logs in Linux

How to Check Crontab logs in Linux

Linux system administrators and developers often use cron jobs to schedule and automate tasks. So they often need to check if a specific cron job was executed or not. For this purpose, you can refer to crontab logs which tracks all cronjobs that are run on your system, along with their status code. In this article, we will learn how to check crontab logs in Linux.

How to Check Crontab logs in Linux

There are several ways to check crontab logs in Linux. You can use any of the following methods. They are available to almost every Linux distribution.

1. Using Syslog

You can use syslog utility to easily check cron jobs. For this purpose, you need to log in as root user or a use with sudo privileges and execute the following command.

# grep 'cron|CRON' /var/log/syslog | less

The syslog file mentioned above logs all commands that are run on your system. We use grep command to filter logs of cron jobs and display them using less command in a paginated manner.

2. Using cron.log file

Many Linux administrators prefer to create a separate log file to track jobs. To do this, open /etc/rsyslog.d/50-default.conf in text editor.

$ vi /etc/rsyslog.d/50-default.conf

Look for the following line.

#cron.* /var/log/cron.log

Uncomment it by removing # at its beginning.

cron.* /var/log/cron.log

Create a new cron log file using text editor.

$ vi /var/log/cron.log

Save and close the empty file. Please note, the file path of your log file should be same as that mentioned in the uncommented line in 50-default.conf file.

Now it will be automatically written to by rsyslog service. Restart rsyslog service with the following command.

$ sudo systemctl restart rsyslog
$ sudo systemctl status rsyslog

You should see Active: active (running) status flag showing that the service is running properly. Now whenever you want to see cron log status, just view the log file with the following command.

$ cat /var/log/cron.log

3. Automatic Cron log Monitoring

You can also automate the process of monitoring the most recent cron log entries. For this, you need to create a shell script that extracts the 50 most recent entries from cron log file and automatically refreshes it every 30 seconds.

Create an empty shell script.

$ vi cronmon

Add the following lines to it.

#!/bin/bash
watch -n 30 tail -n 50 /var/log/cron.log

In the above code, -n 30 tells watch command to run tail command every 30 seconds, whereas -n 50 tells the tail command to extract the most recent 50 entries in cron.log file, every time it is run.

Save and close the file. Make it an executable with the following command.

$ sudo chmod +x cronmon

Now you can run the shell script with the following command. It will keep running and displaying the 50 most recent cron log entries, every 30 seconds.

$ sudo ./cronmon

In this article, we have learnt how to log cron jobs in Linux. You can use them with any Linux distribution.

Also read:

How to Add Directory PATH in Linux
How to Remove PPA in Ubuntu/Debian
How to Add Repository in Ubuntu/Debian
How to Update Ubuntu Kernel to Latest Version
How to Fix SSH Connection Refused Error

Leave a Reply

Your email address will not be published. Required fields are marked *