Versions Compared

Key

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

Cron jobs are a standard method of scheduling tasks to run on your server. Cron is a service running in the background that will execute commands (jobs) at a specified time, or at a regular interval. Jobs and their schedules are defined in a configuration file called a  son el método estándar para ejecutar tareas programadas en su servidor. Cron es un servicio que corre en segundo plano el cual ejecuta tareas (jobs) en un tiempo especifico, o en intervalos regulares. Las tareas y su tiempo de ejecución se definen en un archivo llamado crontab.

Info

If you have Si tiene cPanel, cron jobs can be managed directly in your control panel without directly modifying a crontab. See los trabajos cron se pueden administrar directamente en su panel de control sin modificar directamente un crontab. puede revisar Cron Jobs en cPanel [Como configurarlos] for more information para mas información.

Table of Contents

...

Configuración

Cron jobs are stored in a crontab file by username. These files are stored in /var/spool/cron/crontabs or /var/spool/cron/. These files should not be edited directly. The crontab command should always be used to make changes.

Your crontab can be edited using the following command:

...

The -u can be supplied with any of the other options used above.

Syntax

A crontab file tells the Cron daemon what commands to run, and when to run them. It has three different kinds of lines: cron command, environment setting, and comment. Blank lines, leading spaces, and tabs are ignored.

Comments

Any line whose first non-whitespace character is a pound-sign (#) will be considered a comment. Inline comments are not allowed. Comments are not processed by the Cron daemon.

Environment Setting

Environment setting is in the form of:

...

The MAILTO variable is checked to see if mail needs to be sent. By default, mail is sent to the owner of the crontab. Mail can be sent to another email address by assigning it to MAILTO. If you don't want Cron to send mail, assign MAILTO to the empty string (MAILTO=""). The MAILTO variable will only affect where emails are sent for commands that come after the line it is assigned. If you want to change all the email address of all commands, you would need to assign MAILTO at the top of the file. If you want to change the email address for a single command, you would set MAILTO directly above the command, and then set MAILTO back to its previous value before the next command.

Cron Commands

A cron command has six fields: five date and time fields, and a command field. The Cron daemon checks the schedule of each job every minute. A job is run whenever the time, and date matches the current time and date.

...

Code Block
languagetext
0 0 * * * /path/to/command

Scheduling

The five date and time fields are minutehourday of the monthmonth, and day of the week respectively:

...

Step values may be used with ranges. A step value is written as a range followed by a forward slash (/), and a number. The number will tell the Cron daemon how many times it should skip that job before running it again. The value 0-23/2 in the hour field tells Cron to execute the command every other hour. Step values may be used with asterisks as well so */2 will also tell Cron to run a command every other hour.

Commands

The last field of a cron command is the command to be run. Specifically, everything after the day of the week field until a newline or '%' character is considered to be part of the command, and will be executed by the shell set in the SHELL environment variable (/bin/sh by default).

All '%' characters in a command are converted to newline characters. A literal '%' must be escaped with a preceding backslash (\) e.g. \%. No other characters need to be escaped in a command.

Extensions

There are a handful of special time shorthand that may be used instead of the five date and time fields.

ShorthandValue
@rebootRuns once after a reboot.
@yearly or @annuallyRun once a year (0 0 1 1 *).
@monthlyRun once a month (0 0 1 * *).
@weeklyRun once a week (0 0 * * 0).
@dailyRun once a day (0 0 * * *).
@hourlyRun once an hour (0 * * * *).

Examples

A command that runs every minute:

...

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.

...