285

I have a fresh install of Ubuntu 12.04.1 LTS an a number of servers.

I have not added any cron jobs or edited my crontab on those servers, however, at around the same time for each machine, I get a 75% CPU spike and the following info in my syslog at the time of the spike:

CRON[8380]: (CRON) info (No MTA installed, discarding output)

I have mono-complete installed and am running a service stack webserver.

What is the best way for me to stop this from happening? I would like to be able to remove the CPU spike.

1
  • Every day at 6:25? Those are scripts from /etc/cron.daily/. I have only one trying to send mail: popularity-contest. Look though your scripts and see which script is trying to send mail? That should narrow it down. Then 'chmod 0644 /etc/cron.daily/script-name' to prevent execution.
    – user8290
    Nov 27 '12 at 16:11

12 Answers 12

227

Linux uses mail for sending notifications to the user. Most Linux distributions have a mail service including an MTA (Mail Transfer Agent) installed. Ubuntu doesn't though.

You can install a mail service, postfix for example, to solve this problem.

sudo apt-get install postfix

Or you can ignore it. I don't think the inability of cron to send messages has anything to do with the CPU spike (that's linked to the underlying job that cron is running). It might be safest to install an MTA and then read through the messages (mutt is a good system mail reader).

5
  • 8
    Is postfix a good thing to install? Which MTA is easiest to use?
    – endolith
    Nov 26 '13 at 4:34
  • 4
    postfix is the most widely used mail server for linux, stick to it Dec 3 '14 at 17:03
  • 8
    It is worth pointing out that for use with cron (that is if you don't want to actually send email outwards) during the installation procedure you should answer to configure for local use only.
    – steffen
    Oct 27 '17 at 10:16
  • 1
    the only way I know to check mail is mail provided by mailutils (debian), if there a better way built in to postfix? Jun 15 '19 at 17:30
  • A more simple MTA to use can be msmtp-mta (which is commonly used with msmtp itself).
    – pyrsmk
    Jan 3 '21 at 15:16
118

This happens because your cron jobs are producing output and then the cron daemon tries to email that output to you (i.e. root). If you don't need that output, the easiest way to solve this is to discard it at the crontab:

sudo crontab -e

and add >/dev/null 2>&1 to every job:

* * * * * yourCommand >/dev/null 2>&1
5
  • 14
    The problem with this approach is it doesn't explain the high CPU usage. Cron is clearly trying to be communicative and this is essentially just ignoring the output. I'd be more inclined to handle the output than discard it, just in case there's useful debug info.
    – Oli
    Jan 2 '14 at 14:39
  • 1
    Oli, even though this is an old question, I am experiencing the same exact issue but it is on Raspberry PI. I see bunch of No MTA installed, discarding output message in the log and my program eventually stops running on its own. I believe it is because of CPU spike. It looks like all the answer posted for this question seem to ignore that.
    – ThN
    Jan 18 '17 at 15:37
  • 13
    To keep the output, put the command in a script, and pipe stdout and stderr to logger. For example, yourCommand >/dev/null 2>&1 | logger -t mycmd. This will put the output in syslog for safe keeping, and stop the MTA complaints. Feb 13 '18 at 0:03
  • 2
    @rob : Hiding the output is not a fix.
    – pyrsmk
    Jan 3 '21 at 15:14
  • For people reading @CivMeierFan's comment - don't put the > /dev/null in there :) See Michael Hunter's answer for more details.
    – Nacht
    Jan 4 '21 at 13:42
85

In my case, the message was hinting at a permissions problem with the bash script, but I couldn't see it until I installed an MTA.

As suggested I ran:

sudo aptitude install postfix

I chose "Local" during setup and after running the cron job again:

sudo tail -f /var/mail/<user>

In my case I replaced

<user>

with "root".

I was then able to see the error output related to permissions.

1
  • 7
    Thank you for the extra comment on how to check the mailbox to see errors from the job! Dec 14 '16 at 14:08
60

As stated in an earlier answer, this happens because your cron jobs are producing output, and then the cron daemon tries to email that output to you.  If you don’t want to (or can’t) install an MTA, but you want to see the output, you can redirect the output of the cron job to a log file.  Edit your crontab file with

crontab -e

(use sudo if the issue is with root’s crontab) and add >> /some/log/file 2>&1 after every command, like this:

0 3 * * * cmd  >> /some/log/file 2>&1

If there are multiple commands on a line, separated by ;,  &&  or ||, you should do the above for each command, like this:

0 3 * * * cmd1  >> /some/log/file 2>&1;  cmd2  >> /some/log/file 2>&1

or group them, like this:

0 3 * * * (cmd1;  cmd2)  >> /some/log/file 2>&1

If you want to ignore stdout and capture only stderr, use > /dev/null 2>> /some/log/file instead.  Put the log file wherever you want — your home directory, /var/log, or even /tmp if you’re sure you won’t need to keep it.

Then look at the log file after the job runs.

46

This is an old question but there is an additional answer that is useful in some circumstances.

Pipe the output of your cron command through logger so they end up in the syslog.

It's slightly easier than installing postfix, and it puts this output into syslog alongside your other logs. This command will capture stdout AND stderr so you won't see the No MTA installed message and you'll see all your output in the syslog.

Example cron entry:

0 3 * * * (cmd1;  cmd2) 2>&1 | logger -t mycmd

You can view logs with your tag mycmd using:

grep 'mycmd' /var/log/syslog
4
  • Do pipes work in cron jobs? Feb 13 '18 at 0:05
  • 1
    This works perfectly for me. Logs are available via journalctl. Dec 27 '19 at 4:33
  • 1
    Perfect Answer. Mar 11 '21 at 14:08
  • 1
    This answer should be marked as a solution because it exactly solves the question in a modern way with the Syslog service. Nov 25 '21 at 16:28
45

In crontab add this as first line:

MAILTO=""

This will prevent cron from trying to send an e-mail.

2
  • 8
    Don't put any comments (#) after MAILTO="" or it won't work
    – SBF
    Mar 29 '18 at 9:33
  • Is there a way to set this globally? like in /etc/default/cron
    – Freedo
    Jul 5 '21 at 7:59
27

If you don't want to install an MTA (which I currently have no need for) you can pipe the results of the cron job to a log file.

sudo crontab -e

then with your cron job would look like this.

0 3 * * * /cmd/to/run >> /var/log/somelogfile.log

then you can just tail the log and see what happened

sudo tail -f -n 50 /var/log/somelogfile.log

This is what I have been doing on any server that I see that message in syslog

5
  • 1
    I've done this, but no logging is added to the file.
    – pir
    Aug 6 '17 at 19:18
  • Are you sure that the cron job as been running? Aug 7 '17 at 20:15
  • Yes, the system log shows that it's being started.
    – pir
    Aug 7 '17 at 21:14
  • 3
    You need '2>&1' on the end of the line to capture output to stderr into the file.
    – MattSmith
    Dec 4 '17 at 0:33
  • @MattSmith That doesn't do anything, the file doesn't exist
    – endolith
    Mar 13 '21 at 5:34
11

One side effect of adding /dev/null 2>&1 to the cron job command, is that it will discard both STDERR and STDOUT (Standard Error as well as Output). This works fine if you don't want any emails from cron. But if you want your errors to be emailed to you, use >/dev/null instead. Read this blog post for more explanation.

You'll still need to install an MTA (mail transfer agent) to send the error emails though. Postfix is simple enough to install with: sudo apt-get install postfix

2
  • As far as I understood '>/dev/null' will only send them, and '>/dev/null 2>&1' will doscard all errors? What would I have to use to get errors in log but no mails? I now get no mails (as I want it) but the ugly 'no MTA…'
    – Pit
    Feb 5 '14 at 12:31
  • 2
    afaik, there's no way to log the output other than to send it to emails. The closest thing you can do is to set up postfix for local mail delivery (if you run "sudo apt-get install postfix" it prompts you whether you want to set up local delivery. While this looked like a pain initially, it actually works a lot better. Whenever I log in via ssh, I see a new email on the machine if a previous job has failed. I find it more convenient than having to check the log. Feb 7 '14 at 4:32
4

You can set MAILTO=”” variable at the start of your crontab file. This will also disable email alert. Edit/Open your cron jobs:

$ crontab -e

At the top of the file, enter:

MAILTO=""

https://www.cyberciti.biz/faq/disable-the-mail-alert-by-crontab-command/

1
2
  1. At first, install postfix, that can resolve the problem

    sudo apt-get install postfix
    
  2. If Ubuntu, you can edit the crontab file

    sudo vim /etc/crontab
    
  3. Attention,edit the top file,not any code in the first line,and enter

    MAILTO=root // current system user
    
  4. When cron executes any task,you will get an email

    mail
    
1
  • 1
    Your answer seems to be good, but I couldn't make sense of all of the instructions. Maybe you can improve it.
    – zx485
    Sep 19 '18 at 17:00
1

I had this problem using the Kitematic Docker tools.
Go to the magento container and click on exe.

Then run

apt-get update

This is if you're trying to get magento run on kitematic. The log will show this error on the virtual machine:

need update.

Sorry if this got you lost, but that's how it works. You keep getting lost, but just read about it and the pieces will come together one day. Be patient.

1

I had the problem today, but on containers in an isolated network, so no internet... And I was not able to add file redirect on each job since they were automatically deployed...

I found this quick&dirty hack, but it works!

# echo -e '#!/bin/bash\nlogger -t fake-sendmail' > /usr/sbin/sendmail
# chmod a+x /usr/sbin/sendmail

If it can help someone...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.