check crontab logs

How to Check Crontab logs in Linux

Linux allows you to schedule tasks using Crontab jobs. Very often you need to check crontab logs to see if a specific task was run or not. In this article, we will look at how to check crontab logs in Linux.


How to Check Crontab logs in Linux

Here are the different ways to check crontab logs in Linux. You can use them to monitor cron jobs on your system.


1. Using Syslog

This is very simple way to check crontab logs. Just log in as root or user with sudo privileges and run the following command.

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

syslog logs all commands on your system, including cron jobs. This will list all the cronjobs run on your system.


2. Using cron.log file

The recommended way to monitor cronjobs is to create a separate cron.log file that logs all cron jobs. For this open /etc/rsyslog.d/50-default.conf file in a text editor.

$ sudo 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

Next, create a new cron.log file in /var/log folder

$ sudo vi /var/log/cron.log

Save and close the empty file. It will be automatically written 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 indicating that it is running properly. Whenever you want to see crontab logs just run the following command

$ cat /var/log/cron.log


3. Automate Cron log monitoring

You can even create a simple shell script that automatically logs most recent 50 entries from cron.log and automatically refreshes every 30 seconds.

Create an empty shell script

$ sudo 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 the tail command every 30 seconds. -n 50 tells tail command to get the most recent 50 lines from /var/log/cron.log file.

Save and close the file. Run the following command to make it an executable.

$ sudo chmod +x cronmon

Now you may run this shell script with the following command.

$ sudo ./cronmon

That’s it. In this article, we have explained three ways to log cron jobs. You can use them to find out which cron jobs have run properly and when.

Also read:

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

Leave a Reply

Your email address will not be published.