Ubuntu 16.04 Create a cron.log File To Log crontab Logs

Posted on in Categories , , , last updated January 25, 2017

I am a new Ubuntu Linux 16.04 user. How do I verify that my cron job is executing? I am unable to find /var/log/cron.log file. Where is the cron log file on Ubuntu 16.04 LTS server?

By default installation the cron jobs get logged to a file called /var/log/syslog. You can also use systemctl command to view last few entries. In this quick tutorial you will learn about the default cron log file and how to change or setup a cron.log file to contain just the cron job entries that show up in syslog.

Defaults

Type the following command to view default entries:
$ sudo tail -f /var/log/syslog
OR better use the grep command to find cron job in that file:
$ sudo grep -i cron /var/log/syslog
Sample outputs:

Fig.01: Ubuntu Linux: Where is my crontab log file?
Fig.01: Ubuntu Linux: Where is my crontab log file?

Use systemctl command

You can also use the following command to just see latest CRON task related entries on Ubuntu v16.04 LTS+ only:
$ sudo systemctl status cron

Use journalctl command to display log

Type the following command to see cron logs on Ubuntu v16.04 LTS+ only:
$ sudo journalctl -u cron
$ sudo journalctl -u cron -b | more
$ sudo journalctl -u cron -b | grep something
$ sudo journalctl -u cron -b | grep -i error

Sample outputs:

Aug 21 13:52:01 server2 cron[7510]: Error: bad username; while reading /etc/cron.d/backups
Aug 21 13:52:01 server2 cron[7510]: (*system*backups) ERROR (Syntax error, this crontab file will be ignored)
Aug 21 14:06:01 server2 cron[7510]: Error: bad username; while reading /etc/cron.d/backups
Aug 21 14:06:01 server2 cron[7510]: (*system*backups) ERROR (Syntax error, this crontab file will be ignored)
Aug 22 00:43:01 server2 cron[7510]: Error: bad hour; while reading /etc/cron.d/backups

Where,

  • -u cron : Show messages for the specified systemd unit UNIT called cron
  • -b : Show messages from a specific boot i.e. from the beginning of the journal.

How to enable or create a cron.log file to contain just the CRON entries

Edit the /etc/rsyslog.d/50-default.conf file, enter:
$ sudo vi /etc/rsyslog.d/50-default.conf
### or use a nano text editor ###
$ sudo nano /etc/rsyslog.d/50-default.conf

Find the line:

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

Comment out (uncomment line) in a configuration file as follows:

cron.*                         /var/log/cron.log

Save and close the file. Restart rsyslog service, enter:
$ sudo systemctl restart rsyslog
## verify it ##
$ sudo systemctl status rsyslog

Sample outputs:

* rsyslog.service - System Logging Service
   Loaded: loaded (/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2016-08-22 00:38:44 CDT; 22s ago
     Docs: man:rsyslogd(8)
           
RSyslog Documentation
Main PID: 10855 (rsyslogd) Tasks: 4 Memory: 668.0K CPU: 6ms CGroup: /system.slice/rsyslog.service `-10855 /usr/sbin/rsyslogd -n
Aug 22 00:38:44 server2 systemd[1]: Starting System Logging Service... Aug 22 00:38:44 server2 systemd[1]: Started System Logging Service.

You can now see cron log entries in /var/log/cron.log file:
$ sudo tail -f /var/log/cron.log
$ sudo grep something /var/log/cron.log

Leave a Comment