Ubuntu – 16.04: How to make cron create cron.log and monitor it in real time

16.04cronlogsyslog

I know that cron events are stored in /var/log/syslog and I can use a command like:

sudo cat /var/log/syslog | grep cron 

to see them.

I'd much rather have the system keep a separate cron log file so it doesn't do as much work watching it, as it sorts through all the syslog messages to pull out the cron notifications.

Then I want to use the watch command so I always have an open terminal window showing cron activity.

How do I make the system keep a separate /var/log/cron.log file?

Best Answer

Here is the simplest way to do it:

--> Make a change in /etc/rsyslog.d/50-default.conf with your favorite editor:

sudo gedit /etc/rsyslog.d/50-default.conf

Use Ctrl-f (find) and type cron to find the line that says

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

Remove the # from that line. Then restart the service:

service rsyslog restart

From then on, all cron-related output will go to /var/log/cron.log


In order to prevent cron from sending summary emails, place this line at the beginning of your crontab file:

crontab -e
(Insert first line)
MAILTO=""

Then to watch it in near-real-time, first create a wcron command:

echo "#!/bin/bash" >wcron
echo "watch -n 10 tail -n 25 /var/log/cron.log" >>wcron
chmod +x wcron
sudo cp wcron /usr/sbin
  • watch -n 10 tells it to refresh the page every 10 seconds
  • tail -n 25 tells it to display the last 25 entries

Whenever you want to monitor cron in near-real-time in a terminal window, enter:

wcron

This is handy to have open in one of the 4 virtual desktops, perhaps along with System Monitor.


If you want to see more than just the launch times of longer jobs, it is easy to make the cron.log show both script start times and script end times, along with non-zero exit status messages.

How to change cron log level?

PARAPHRASED EXCERPT:

From Ubuntu 15.04 on, upstart using /etc/init/*.conf is replaced by systemd using configuration files in /lib/systemd/system/ and /etc/systemd/system/. Although a file /etc/init/cron.conf is still existent in Ubuntu 16.04, the script normally in use to start cron now is /lib/systemd/system/cron.service.

To add the -L 15 option, open the editor by using:

sudo systemctl edit --full cron

OR I RECOMMEND:

sudo gedit /lib/systemd/system/cron.service

and replace the line

ExecStart=/usr/sbin/cron -f $EXTRA_OPTS

with

ExecStart=/usr/sbin/cron -L 15 -f $EXTRA_OPTS

Then reload the configuration:

sudo systemctl restart cron

It will load it on boot also.


This enables wcron to show all the information you need to watch your cron system as it runs your scripts on schedule.


One final idea for monitoring cron is to teach your scripts to speak.

16.04 LTS How to make the system announce the time at the top of the hour with eSpeak

I have many of my scripts announce when they start and when they stop, or if they encounter errors.

Volume may be adjusted in the espeak command so they can be subtle.

Related Question