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

last updated in Categories , , ,

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 Linux 16.04 LTS server or above? How can I create a cron.log file on Ubuntu Linux 16.04/18.04 LTS cloud server?

Cron jobs allows Linux and Unix users to run commands or scripts at a given date and time. 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 or create 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 to view cron logs

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)
           www.rsyslog.com/doc
 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

Sample outputs:

Jul 14 18:34:01 db1 cron[20025]: (CRON) INFO (pidfile fd = 3)
Jul 14 18:34:02 db1 cron[20025]: (CRON) INFO (Skipping @reboot jobs -- not system startup)
Jul 14 18:35:02 db1 CRON[20065]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)

Conclusion

You learned how to create a cron.log file on Ubuntu Linux server.

This entry is 16 of 16 in the Linux Cron Jobs Howto & Tutorial series. Keep reading the rest of the series:
  1. What is cron on a Linux or Unix-like systems?
  2. How To Add Jobs To cron Under Linux or UNIX?
  3. Verify crond Daemon And Cronjobs Are Running
  4. Start Restart and Stop The Cron or Crond Service
  5. List / Display All Cron Jobs
  6. Linux / UNIX Crontab File Location
  7. Change Crontab Email Settings ( MAILTO )
  8. Disable The Mail Alert By Crontab Command On a Linux or Unix-like Systems
  9. At What Time Cron Entries In cron.daily, cron.weekly, cron.monthly Run?
  10. Execute Cron Job After System Reboot
  11. Setup and Run PHP Script As A Cron Job
  12. Run crontab job every minute on a Linux or Unix-like system
  13. Running crontab (cron jobs) Every 10 Minutes
  14. Cron Job Script Execution on the Last Day of a Month
  15. Execute / Run crontab (cron jobs) every 1 minute
  16. Ubuntu create cron.log file

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive