Sunday, February 2, 2014

How to disable the email alert in CRON and delete the alert emails

Hi,

As always something new happend. Today I have time to explain how to disable the email alert that CRON send with the output of a command... and How to delete the emails from console?

First, to avoid reveiving the email alert I recoment to add at the end of every cron line 

1 * * * * your_script_here  > /dev/null 2>&1

And what is that? Simple. Adding this piece of code at the end of the command we indicate the system to send the stdout and stderr to /dev/null. Another shorter way is

1 * * * * your_script_here  &> /dev/null

which has the same effect. If you want to learn more about how to redirect the stderr and stdout you can create a doof script like this (kk.sh):

#!/bin/bash
echo "This is stdout." >&1
echo "This ist stderr." >&2

and try different commands to figure out how it works. For example:
$ sh kk.sh 1>/dev/null
This ist stderr.
$ sh kk.sh 2>/dev/null
This is stdout.
$ sh kk.sh 2>&1 > /dev/null
This ist stderr.

Now to finish this post... how to delete the emails from the console? Easy!.
$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/mail/texnological": 9458 messages 8543 unread

? delete 1-9458
? q
 As you can see, we need only to use delete and write the email range we want to delete.

Hope help!!