🔭 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.
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:
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.
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:
Save the empty file and exit. Then restart the rsyslog service:
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:
Add the following line in the file. Then save and exit the file.
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:
And finally, copy this file in /usr/sbin location using the following command:
Now to watch cron log in real-time, simply type watchcron in the terminal and
hit enter.
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.
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.
In this quick tutorial, we will take a look at how to set up a cron job to run at a specific time.
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.