The cron
or crontab
are a command and service used to run scheduled commands and scripts in Linux operating systems. The cron is used to set scheduled commands and execute them at specified times or intervals. This operation is error prone where we may need to check if the specified cron job is executed properly.
Cron Logs via /var/log/syslog
By default, the cron or cronjob logs are stored inside the syslog
file. The syslog file is used as the default log file for most of the services and system-related events. The syslog is located inside the /var/log
. The cat and grep commands can be used to filter logs. In the following example, we filter only cron and crontab logs.
$ cat /var/log/syslog | grep cron
We can also filter specific terms inside the cron logs. We just add an extra grep with the search term. In the following example, we search the term “root” inside the cron logs.
$ cat /var/log/syslog | grep cron | grep root
Cron Logs via /var/log/cron.log
The more systematic and reliable way to log cron and crontab. The rsyslog
configuration can be used to log cron and crontab into separate files with a specific configuration. First, we should install the rsyslog if it is not installed.
$ sudo apt install rsyslog
We should configure the logging for the crontab by editing this file.
$ sudo nano /etc/rsyslog.d/50-default.conf
The following line should be uncommented in order to enable cron or crontab to log into the file named “cron.log”.
cron.* /var/log/cron.log
In order to make this configuration to effective, we should restart the rsyslog service with the following command.
$ sudo systemctl restart rsyslog
Check if the rsyslog service is running with the following systemctl command.
$ systemctl status rsyslog
As the last step, we can create the log file with the touch
command below.
$ sudo touch /var/log/cron.log
The cron.log file can be displayed with the cat
command like below.
$ cat /var/log/cron.log
Alternatively we can interactively track the cron or crontab log file with the watch
command.
$ watch -f /var/log/cron.log