🔭 Want to get alerted when your Cron doesn’t run correctly?
Go to Better Stack and start monitoring in 5 minutes.
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.
Go to Better Stack and start monitoring in 5 minutes.
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
directory to /var/log/cron
and you are good to go.
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 /var/log/cron.log
file.
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:
cron.* /var/log/cron.log
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 rsyslog
service:
sudo systemctl restart rsyslog
At this point, all cron logs are stored in the /var/log/cron.log
file.
To view cron logs in real-time, create watchcron
file using the following
command:
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 watchcron
file:
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
hit enter.
watchcron
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.
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.
In this quick tutorial, we will take a look at how to set up a cron job to run at a specific time.
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.
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usWrite a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github