In this tutorial, we'll look at how to check cron logs and monitor jobs in real time in Ubuntu 18.04. A cron job is a task scheduler used for automation of repetitive tasks in a Linux environment. It is normally executed at a specific time and date as dictated by the system administrator. Cron logs really help admin to verify if the cron jobs have run or not at the particular time.
Usually, you can view cron events using
cat /var/log/syslog | grep cron
Output
However, it's more preferable to have a separate cron.log file which you can use to monitor cron events.
Setting up cron.log file
First off go to /etc/rsyslog.d/50-default.conf
file and search for this line #cron.* /var/log/cron.log
cat /etc/rsyslog.d/50-default.conf | grep cron
Output
#cron.* /var/log/cron.log # cron,daemon.none;\
Using your favorite text editor, open the file and uncomment the line as shown in line 10
Next, create cron.log file
vi /var/log/cron.log
Restart the rsyslog service
systemctl restart rsyslog
Check the status of rsyslog to ensure it is running
systemctl status rsyslog
Sample Output
From then henceforth all log will be saved at /var/log/cron.log
file.
Next, we are going to create a watchcron command which we will invoke to have a glimpse at the cron events in real time.
create the watchcron file
nano watchcron
Add the following
#!/bin/bash watch -n 10 tail -n 25 /var/log/cron.log
Save and exit
Note :
watch -n 10 refreshes the page every 10 seconds.
tail -n 25 displays the last 25 entries.
Give the watchcron file execute permissions
chmod +x watchcron
Copy it to /usr/sbin as shown
cp watchcron /usr/sbin
To watch real-time cron events run
watchcron
Sample Output
Every 10.0s: tail -n 25 /var/log/cron.log Sun May 13 14:00:19 2018 May 13 06:47:01 ip-172-31-41-251 CRON[26305]: (root) CMD (test -x /usr/sbin/anac ron || ( cd / && run-parts --report /etc/cron.weekly )) May 13 07:17:01 ip-172-31-41-251 CRON[26993]: (root) CMD ( cd / && run-parts - -report /etc/cron.hourly) May 13 08:17:01 ip-172-31-41-251 CRON[28255]: (root) CMD ( cd / && run-parts - -report /etc/cron.hourly) May 13 09:17:01 ip-172-31-41-251 CRON[29487]: (root) CMD ( cd / && run-parts - -report /etc/cron.hourly) May 13 10:11:01 ip-172-31-41-251 CRON[30655]: (root) CMD (/sbin/reboot) May 13 10:11:34 ip-172-31-41-251 cron[1226]: (CRON) INFO (pidfile fd = 3) May 13 10:11:34 ip-172-31-41-251 cron[1226]: (CRON) INFO (Running @reboot jobs) May 13 10:17:01 ip-172-31-41-251 CRON[2506]: (root) CMD ( cd / && run-parts -- report /etc/cron.hourly) May 13 11:17:01 ip-172-31-41-251 CRON[2986]: (root) CMD ( cd / && run-parts -- report /etc/cron.hourly) May 13 12:17:01 ip-172-31-41-251 CRON[3252]: (root) CMD ( cd / && run-parts -- report /etc/cron.hourly) May 13 13:17:01 ip-172-31-41-251 CRON[3513]: (root) CMD ( cd / && run-parts -- report /etc/cron.hourly)
If you don't wish to receive emails from cron, append this line at the beginning of your crontab file
MAILTO=""
In this article, we have shown you how to monitor cron jobs logs in real time using a single command. Feel free to try out the procedure and give us your feedback. Thank you.
Hi Jamie!
A minor correction here:
Copy it to "/usr/Sbin" as shown
Sure Nazim. It's /usr/sbin as indicated in the command. Thanks for that.
"cat <file> |grep <thing>"
It's "grep <thing> <file>"
Read your man pages man
If you wanted to watch it in real time then couldn't you just use:
tail -n 25 -f /var/log/cron.log
No need to use watch
I had to change group of /var/log/cron.log from root to adm