Lorixen.
EN

Cron job emails: stop the flood, keep the useful ones

Short answer: Cron emails whatever a job prints. Set MAILTO at the top of your crontab to choose where that mail goes, or MAILTO="" to send none, then make each job quiet when it succeeds: send normal output to a log file and let only errors reach the email. If you rely on those emails as your alert, check the server can actually deliver mail, because on many servers cron's messages go to a local mailbox nobody reads.

Two opposite problems, one cause

Small teams usually hit one of these:

  • The flood. Every five minutes a job prints "Sync complete: 0 changes", and someone's inbox has thousands of cron emails. Real failures drown in the noise, and eventually someone sets up a filter that hides them all.
  • The silence. A backup script has been failing for weeks, printing errors that went into a local mail file on the server or nowhere at all.

Both come from the same behaviour: cron collects anything a command writes to standard output or standard error and, if there's any, mails it.

How MAILTO works

The crontab(5) manual describes the rules:

  • If MAILTO is defined and non-empty, mail is sent to that address.
  • If MAILTO is defined but empty (MAILTO=""), no mail is sent.
  • If it isn't set, mail goes to the owner of the crontab, which usually means a local mailbox on the server.

The same page describes MAILFROM, which sets the envelope sender address where supported; otherwise the executing user's name is used. Both variables are expanded, so you can include the user's name in them.

A crontab that sends job output to a shared address looks like this:

MAILTO=ops@example.ie
MAILFROM=cron@server1.example.ie
30 2 * * * /srv/scripts/nightly-backup

Put the settings at the top; they apply to the lines below them.

Make successful runs quiet

The most effective change is in the jobs themselves. A well-behaved scheduled script prints nothing when everything worked and prints a clear message to standard error when something didn't. Then email means "look at this".

When you can't change the script, redirect its normal output:

*/5 * * * * /srv/scripts/sync-stock >> /var/log/sync-stock.log

Standard output goes to the log; anything written to standard error still reaches cron and is mailed. If you want everything logged and nothing mailed:

*/5 * * * * /srv/scripts/sync-stock >> /var/log/sync-stock.log 2>&1

Only do that if something else watches the job, such as a heartbeat check or log alert, or failures will go unseen again.

Some teams use a small wrapper that captures output and only prints it if the command exits with an error. Several Linux utility packages include one, or you can write a few lines of shell to do it.

Watch out for the percent sign

The crontab manual notes that a % in the command is turned into a newline, and anything after the first one is sent to the command as input. A command like date +%Y-%m-%d in a crontab won't work as written. Escape it as \% or move the command into a script. It's a classic cause of "it works in the terminal but not in cron", and the error ends up in those emails.

Make sure the email can actually arrive

Setting MAILTO to a real address doesn't guarantee delivery. The server needs a working mail setup: a mail transfer agent or a relay through your email provider or transactional email service. Messages sent directly from a cloud server's IP are often rejected or land in spam.

Test it: add a temporary job that runs a minute from now and prints a line, then check the inbox and the spam folder. If nothing arrives, look at the server's mail log or the local mailbox for the crontab's owner.

Logs need housekeeping too

Redirecting output to log files moves the problem from your inbox to your disk. Add each new log to your log rotation configuration so it's compressed and trimmed regularly. Include a timestamp in each line your script writes, so you can match log entries to run times.

Time zones in timestamps

The crontab manual mentions CRON_TZ, which, where your cron supports it, lets you write schedule times in a specific time zone for that crontab. It also notes that times written to the cron log use the daemon's local time zone. If your server runs in UTC and you're in Ireland, keep that in mind when matching an email or log entry to a job.

A quick audit of your crontab

List every scheduled entry with crontab -l for each user that has one, plus any files under the system cron folders. For each line, note what it does, who should hear about failures, and whether it prints on success. Remove jobs nobody recognises after checking with the team; old entries that call scripts which no longer exist are a steady source of error mail.

A worked example

An illustrative Limerick online bookshop ran a stock sync every five minutes and a nightly backup from the same crontab. The sync printed a summary on every run, so the owner's inbox received hundreds of cron emails a day and she filtered them all into an unread folder. When the backup started failing after a disk change, its error emails went into the same folder. The developer redirected the sync's normal output to a rotated log, left errors going to cron, set MAILTO to a shared ops address and tested delivery. The inbox went quiet, and the first email that arrived after that was a genuine failure.

FAQ

Does MAILTO="" stop errors from being reported?

Yes. It stops all cron mail, so only use it if you have another way to see failures.

Can I send different jobs to different addresses?

Yes. A later MAILTO line in the same crontab applies to the entries that follow it.

Why do I get emails even though my script works?

It's printing something on success. Make it quiet, or redirect its standard output to a log.

Is email the best alert for scheduled jobs?

It's a start. For important jobs, add a check that alerts you when a job doesn't run at all.

If your scheduled jobs are noisy or failing quietly, send us your crontab and we'll suggest a cleaner setup.

Sources

WhatsAppBook a call