Introduction
This guide explains how you can silence unwanted cronjob notifications.
Method 1: Output Redirection
It is possible to redirect the cron command's STDOUT and STDERR to /dev/null so that they will not be mailed to you.
The following is an example of what this would look like to silence both STDOUT (Regular output) and STDERR (Errors from the script) so that you never get emails from that script:
15 */2 * * * /path/to/your/script >/dev/null 2>&1
The following is an example of silencing only STDOUT so that you only get error emails:
15 */2 * * * /path/to/your/script >/dev/null
The following is an example of silencing only STDERR so that you only get non-error emails:
15 */2 * * * /path/to/your/script 2>/dev/null
Method 2: The MAILTO variable
You may also just set the MAILTO variable so that it is blank to prevent emails from being sent.
This example shows silencing mail only for SCRIPT--2:
MAILTO="youremail@example.tld"
15 */2 * * * /path/to/your/SCRIPT--1
MAILT0=""
15 */2 * * * /path/to/your/SCRIPT--2
MAILTO="youremail@example.tld"
15 */2 * * * /path/to/your/SCRIPT--3
This example shows silencing mail only for SCRIPT--3:
MAILTO="youremail@example.tld"
15 */2 * * * /path/to/your/SCRIPT--1
15 */2 * * * /path/to/your/SCRIPT--2
MAILTO=""
15 */2 * * * /path/to/your/SCRIPT--3
This example shows silencing mail for all scripts:
MAILTO=""
15 */2 * * * /path/to/your/SCRIPT--1
15 */2 * * * /path/to/your/SCRIPT--2
15 */2 * * * /path/to/your/SCRIPT--3
It's important to keep in mind that the cron file is read from top to bottom, so any environment variables that you set at the top, will be overridden by subsequent variables of the same name below them, which explains the behavior shown above.
Combination of Method 1 and Method 2
You can actually get a very fine selection of Cron email notifications by combining the two methods.
For example, SCRIPT--1 will only send error output to user1@example.tld, SCRIPT--2 is totally silent, and SCRIPT--3 will only send non-error output to user2@example.tld.
MAILTO="user1@example.tld"
15 */2 * * * /path/to/your/SCRIPT--1 >/dev/null
MAILT0=""
15 */2 * * * /path/to/your/SCRIPT--2
MAILTO="user2@example.tld"
15 */2 * * * /path/to/your/SCRIPT--3 2>/dev/null
Actually Making the Edits
You can take what you've learned in this guide and combine it with the following guides to actually make the edits:
- How to Configure the MAILTO environment variable to enable cron notifications on the command line
- How to edit the root Crontab
- How to Create a Cron Job in the cPanel User Interface
Other Cron Related Guides
How to troubleshoot user-level cronjob issues
How to verify if a cronjob was run