I am trying to troubleshoot why my cron task isn't working sometimes. Where does cron store log files by default? Is it /var/log/syslog ?
I have looked at that file and it is empty. Do I need to set something up?
I am trying to troubleshoot why my cron task isn't working sometimes. Where does cron store log files by default? Is it /var/log/syslog ?
I have looked at that file and it is empty. Do I need to set something up?
As default, cron's logs saved in /var/log/syslog.
It depends on rsyslogd configuration. You can change it:
Go to rsyslog config
cd /etc/rsyslog.d/
sudo nano 50-default.conf
Uncoment line:
#cron.* /var/log/cron.log
Save file and restart rsyslog
sudo service rsyslog restart
Restart your cron daemon for get it's messages from new file
sudo service cron restart
When cron
doesn't work, it'll send out a mail to the user root
. The only problem is: you don't have the software to send / store the mail in mailboxes.
But have no fear, Postfix
is here!
sudo apt-get update sudo apt-get install postfix heirloom-mailx
When install postfix
, it'll ask you how to you want to setup it up. On the first screen, select local only
and proceed with the defaults for everything else.
Now, using your favorite editor, edit /etc/aliases
. It'll look something like this in the beginning:
# See man 5 aliases for format postmaster: root
What this means is that any mail sent to postnaster
will now be sent to root
as well. In this case, we want any mail sent to root
(for the cron mails and any other system mail) to be sent to username
(us).
So, edit /etc/aliases
to look like:
# See man 5 aliases for format postmaster: root root: norman
(Replace norman
with your username obviously. Unless you have the same name/username as me. :) )
After that's all said and done, run the following command to push
the changes:
sudo newaliases
Now, after that run:
sudo dpkg-reconfigure postfix
You'll get the same screen you had before when installing postfix
. Run through the defaults (Local Only
, etc..). When you get to the part when it asks for the root and postmaster alias, just make sure it's the same as the one you added to /etc/aliases
above. Then, continue running through the defaults.
When you're done, run the following command to restart postfix
and get underway!
sudo service postfix restart
Now, if cron has an error, it'll be mailed to you. But, you're probably wondering, how in the world do I check my (local) mail?
To do that, run the command:
That simple. If there's no mail it'll say No mail for <username>
. Otherwise you'll get a neat terminal interface to use. See the man page for information on how to interact with your inbox.
Or, if you prefer, you can access your local man page by using:
man mail
And now, you're done! :)
P.S. You should read this to learn more about the cron
issue.
When running mail
I got the following response:
The program 'mail' is currently not installed. To run 'mail' please ask your administrator to install the package 'mailutils'
Instead I found the errors / mails stored in /var/mail/root
#to check all job run timeline
cat /var/log/syslog | grep CRON
#to check each job full log
cd /var/mail/
ll
-rw-rw---- 1 ubuntu mail 9342 Dec 31 07:35 ubuntu
less ubuntu
#in the above file you can find the whole log
just shift + g so you can see all new logs text
#to check live file / log update
tail -f /var/mail/ubuntu
It is in /var/log/syslog
by default.
But it can be set up to create a separate cron.log, which is more useful.
This Q&A describes the process:
16.04: How do I make cron create cron.log and monitor it in real time?
Also in this answer is the instructions to create a wcron
command that displays it is near-real-time. Plus, it links to another answer,
that shows how to change the log level to include more than just the start of jobs - level 15 will show errors and end time, also.
crond
mails the owner of the job, not necessarily root, so if it is your own job you installed withcrontab -e
then you get the mail. This is also true whether or not the job "fails"... it simply emails you a transcript of any output the job had. – psusi May 17 '15 at 23:58