Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagetext
30 17 1-7 * 2 /path/to/command

Output

As previously mentioned, the Cron daemon will email the output of the command to user's email address, or the address set by MAILTO whenever a cron job is executed. This can become a problem however if a command is run frequently. Stopping the emails is an option by setting MAILTO="", but you will then lose the output of the commands. Shell redirection can be used to have the output written to a file instead of being sent as an email.

Como se mencionó anteriormente, el  Cron daemon enviará por correo electrónico la salida del comando a la dirección de correo electrónico del usuario o a la dirección establecida por MAILTO cada vez que se ejecute un trabajo cron. Sin embargo, esto puede convertirse en un problema si un comando se ejecuta con frecuencia. Detener los correos electrónicos es una opción que se puede configurar como MAILTO="", pero luego perderá la salida de los comandos. La redirección de Shell se puede usar para que la salida se escriba en un archivo en lugar de enviarse como un correo electrónico.

El siguiente comando escribirá en un archivo de registro cada horaThe following command will write to a log file every hour:

Code Block
languagetext
0 * * * * echo "Hourly message." > message.log

The command that gets executed is El comando que se ejecuta es echo "Hourly message.". The remaining part of the line, > message.log, redirects the output to a file named message.log. It's important to note that this will replace whatever is in message.log with Hourly message. The >> operator will append to the end of a file instead of overwriting itLa parte restante de la línea, > mensaje.log, redirige la salida a un archivo llamado mensaje.log. Es importante tener en cuenta que esto reemplazará lo que esté en message.log con Hourly message. El >> operador agregará al final de un archivo en lugar de sobrescribirlo:

Code Block
languagetext
0 * * * * echo "Hourly message." >> message.log


If this cron job ran 3 times, the contents of message.log would look like thisSi este trabajo cron se ejecutó 3 veces, el contenido de message.log se vería así:

Code Block
languagetext
Hourly message.
Hourly message.
Hourly message.

If you don't care about the normal output of a cron job, and only want to be notified when it encountered an error, you can redirect the output to Si no le importa la salida normal de un trabajo cron y solo desea que se le notifique cuando encuentre un error, puede redirigir la salida a /dev/null.

Code Block
languagetext
0 * * * * /path/to/my/script > /dev/null

...

Note

/dev/null is a special device file in Linux (and other Unix-like operating systems) that is used when discard output. Anything that is written to this file just gets thrown away by the system.

Redirection can be used to prevent emails from being sent as well by sending both normal output and error messages to La redirección también se puede utilizar para evitar que se envíen correos electrónicos mediante el envío de mensajes de salida normales y de error a /dev/null instead of setting  configurado desde MAILTO="".

Code Block
languagetext
0 * * * * /path/to/my/script 2>&1 > /dev/null

2>&1 in short says to treat error messages like normal output. So  en resumen dice que trate los mensajes de error como una salida normal. Entonces 2>&1 > /dev/null can be read as "treat error messages like normal output, and throw away normal output se puede leer como "tratar los mensajes de error como una salida normal y desechar la salida normal".