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.
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.
cat /var/log/syslog | grep cron
This applies to Ubuntu and Debian. On CentOS, simply replace the system log
/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:
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
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:
We are not done yet. Now we need to create the
/var/log/cron.log file. To do
this, simply run the following command:
sudo nano /var/log/cron.log
Save the empty file and exit. Then restart the
sudo systemctl restart rsyslog
At this point, all cron logs are stored in the
How to watch cron logs in real-time?
To view cron logs in real-time, create
watchcron file using the following
sudo nano watchcron
Add the following line in the file. Then save and exit the file.
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
sudo chmod +x watchcron
And finally, copy this file in
/usr/sbin location using the following command:
sudo cp watchcron /usr/sbin
Now to watch cron log in real-time, simply type
watchcron in the terminal and
How to list and view all current cron jobs?
Cron is a command-line job scheduler on Unix-like systems. It allows you to run automated tasks in the background and it's especially useful for repetitive jobs.Questions
How to set up a cron job for a specific time and date?
In this quick tutorial, we will take a look at how to set up a cron job to run at a specific time.Questions
How to get cron job errors in email with MAILTO?
One of the neat features of Cron is the ability to send emails when an error occurs during the execution of the cronjob. This can be done using the `MAILTO` environmental variable. When executing cronjob, any output is mailed to the owner of the crontab or to the user or email address specified in the `MAILTO` environment variable in the crontab, if such exists.Questions
How to prevent duplicate cron jobs from running?
Sometimes you may find that duplicate cronjobs are running at the same time. This may happen when the cronjob takes longer to complete than its execution interval. Here is a simple way to prevent this from happening ever again.Questions
We are hiring.
Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.
Help us in making the internet more reliable.
Help us with developer education and get paid.
Reliability is the
Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.Explore Better Stack