# How to view and read cron logs on Ubuntu, Debian and CentOS?

Cron can generate logs, which are very useful in troubleshooting your cron jobs.
In this quick tutorial, we will take a look at cron logs – how to find them and
how to read them.

[info]
## 🔭 Want to get alerted when your Cron doesn’t run correctly?
Go to [Better Stack](https://betterstack.com/uptime/) and start monitoring in 5 minutes.
[/info]

## Where are cron logs stored?

By default, all cron logs are stored in the main system log which is located in
`/var/log/syslog` on Ubuntu and Debian systems and in `/var/log/cron` on CentOS.

You can filter cron logs in the system log by running the grep command.

```bash
cat /var/log/syslog | grep cron
```

This applies to Ubuntu and Debian. On CentOS, simply replace the system log
directory to `/var/log/cron` and you are good to go.

## How to set up cron.log file?

Another way you can monitor cron logs is to set up separate log file especially
for cron logs.

To do this, open the `/etc/rsyslog.d/50-default.conf` configuration file in the
`nano` editor using the following command:

```bash
sudo nano /etc/rsyslog.d/50-default.conf
```

This will open the configuration file for editing. In this file, uncomment the
highlighted line to enable logging cron logs into the `/var/log/cron.log` file.

```bash
Output:
#  Default rules for rsyslog.
#
#  For more information see rsyslog.conf(5) and /etc/rsyslog.conf

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*                 /var/log/auth.log
*.*;auth,authpriv.none          -/var/log/syslog
#cron.*                         /var/log/cron.log
#daemon.*                       -/var/log/daemon.log
kern.*                          -/var/log/kern.log
#lpr.*                          -/var/log/lpr.log
mail.*                          -/var/log/mail.log
#user.*                         -/var/log/user.log

#
# Logging for the mail system.  Split it up so that
# it is easy to write scripts to parse these files.
#
#mail.info                      -/var/log/mail.info
#mail.warn                      -/var/log/mail.warn
mail.err                        /var/log/mail.err

#
# Some "catch-all" log files.

...
```

The highlighted line should look like this:

```bash
cron.*                         /var/log/cron.log
```

We are not done yet. Now we need to create the `/var/log/cron.log` file. To do
this, simply run the following command:

```bash
sudo nano /var/log/cron.log
```

Save the empty file and exit. Then restart the `rsyslog` service:

```bash
sudo systemctl restart rsyslog
```

At this point, all cron logs are stored in the `/var/log/cron.log` file.

## How to watch cron logs in real-time?

To view cron logs in real-time, create `watchcron` file using the following
command:

```bash
sudo nano watchcron
```

Add the following line in the file. Then save and exit the file.

```bash
watch -n 10 tail -n 15 /var/log/cron.log
```

This will refresh the logs event page after 10 seconds and displays the last 15
events on the page.

Add the executable permission to the newly created `watchcron` file:

```bash
sudo chmod +x watchcron
```

And finally, copy this file in `/usr/sbin` location using the following command:

```bash
sudo cp watchcron /usr/sbin
```

Now to watch cron log in real-time, simply type `watchcron` in the terminal and
hit enter.

```bash
watchcron
```

[ad-logs]