Salt states 的cron 类型

Management of cron, the Unix command scheduler

Cron declarations require a number of parameters. The following are the parameters used by Salt to define the various timing values for a cron job:

  • minute
  • hour
  • daymonth
  • month
  • dayweek (0 to 6 are Sunday through Saturday, 7 can also be used for Sunday)

警告

Any timing arguments not specified take a value of *. This means that setting hour to 5, while not defining the minute param, will result in Salt adding a job that will execute every minute between 5 and 6 A.M.!

Additionally, the default user for these states is root. Therefore, if the cron job is for another user, it is necessary to specify that user with the user parameter.

A long time ago (before 2014.2), when making changes to an existing cron job, the name declaration is the parameter used to uniquely identify the job, so if an existing cron that looks like this:

date > /tmp/crontest:
  cron.present:
    - user: root
    - minute: 5

改变它成为这样:

date > /tmp/crontest:
  cron.present:
    - user: root
    - minute: 7
    - hour: 2

存在的cron 将被更新掉,但是如果计划任务内容有改变,那么一个新的cron 将被添加到用户的crontab里面。

The current behavior is still relying on that mechanism, but you can also specify an identifier to identify your crontabs:

date > /tmp/crontest:
  cron.present:
    - identifier: SUPERCRON
    - user: root
    - minute: 7
    - hour: 2

2014.1.2 新版功能.

And, some months later, you modify it:

superscript > /tmp/crontest:
  cron.present:
    - identifier: SUPERCRON
    - user: root
    - minute: 3
    - hour: 4

2014.1.2 新版功能.

The old date > /tmp/crontest will be replaced by superscript > /tmp/crontest.

Additionally, Salt also supports running a cron every x minutes very similarly to the Unix convention of using */5 to have a job run every five minutes. In Salt, this looks like:

date > /tmp/crontest:
  cron.present:
    - user: root
    - minute: '*/5'

The job will now run every 5 minutes.

Additionally, the temporal parameters (minute, hour, etc.) can be randomized by using random instead of using a specific value. For example, by using the random keyword in the minute parameter of a cron state, the same cron job can be pushed to hundreds or thousands of hosts, and they would each use a randomly-generated minute. This can be helpful when the cron job accesses a network resource, and it is not desirable for all hosts to run the job concurrently.

/path/to/cron/script:
  cron.present:
    - user: root
    - minute: random
    - hour: 2

0.16.0 新版功能.

Since Salt assumes a value of * for unspecified temporal parameters, adding a parameter to the state and setting it to random will change that value from * to a randomized numeric value. However, if that field in the cron entry on the minion already contains a numeric value, then using the random keyword will not modify it.

Added the opportunity to set a job with a special keyword like '@reboot' or '@hourly'.

/path/to/cron/script:
  cron.present:
    - user: root
    - special: @hourly

The script will be executed every reboot if cron daemon support this option.

/path/to/cron/otherscript:
  cron.absent:
    - user: root
    - special: @daily

This counter part definition will ensure than a job with a special keyword is not set.

salt.states.cron.absent(name, user='root', identifier=False, special=None, **kwargs)

Verifies that the specified cron job is absent for the specified user; only the name is matched when removing a cron job.

name
The command that should be absent in the user crontab.
user
The name of the user whose crontab needs to be modified, defaults to the root user
identifier
Custom-defined identifier for tracking the cron line for future crontab edits. This defaults to the state id
special
The special keyword used in the job (eg. @reboot, @hourly...)
salt.states.cron.env_absent(name, user='root')

Verifies that the specified environment variable is absent from the crontab for the specified user

name
The name of the environment variable to remove from the user crontab
user
The name of the user whose crontab needs to be modified, defaults to the root user
salt.states.cron.env_present(name, value=None, user='root')

Verifies that the specified environment variable is present in the crontab for the specified user.

name
The name of the environment variable to set in the user crontab
user
The name of the user whose crontab needs to be modified, defaults to the root user
value
The value to set for the given environment variable
salt.states.cron.file(name, source_hash='', user='root', template=None, context=None, replace=True, defaults=None, backup='', **kwargs)

Provides file.managed-like functionality (templating, etc.) for a pre-made crontab file, to be assigned to a given user.

name

The source file to be used as the crontab. This source file can be hosted on either the salt master server, or on an HTTP or FTP server. For files hosted on the salt file server, if the file is located on the master in the directory named spam, and is called eggs, the source string is salt://spam/eggs

If the file is hosted on a HTTP or FTP server then the source_hash argument is also required

source_hash
This can be either a file which contains a source hash string for the source, or a source hash string. The source hash string is the hash algorithm followed by the hash of the file: md5=e138491e9d5b97023cea823fe17bac22
user
The user to whom the crontab should be assigned. This defaults to root.
模板
If this setting is applied then the named templating engine will be used to render the downloaded file. Currently, jinja and mako are supported.
context
Overrides default context variables passed to the template.
replace
If the crontab should be replaced, if False then this command will be ignored if a crontab exists for the specified user. Default is True.
defaults
Default context passed to the template.
backup
Overrides the default backup mode for the user's crontab.
salt.states.cron.present(name, user='root', minute='*', hour='*', daymonth='*', month='*', dayweek='*', comment=None, commented=False, identifier=False, special=None)

Verifies that the specified cron job is present for the specified user. For more advanced information about what exactly can be set in the cron timing parameters, check your cron system's documentation. Most Unix-like systems' cron documentation can be found via the crontab man page: man 5 crontab.

name
The command that should be executed by the cron job.
user
The name of the user whose crontab needs to be modified, defaults to the root user
minute
The information to be set into the minute section, this can be any string supported by your cron system's the minute field. Default is *
hour
The information to be set in the hour section. Default is *
daymonth
The information to be set in the day of month section. Default is *
month
The information to be set in the month section. Default is *
dayweek
The information to be set in the day of week section. Default is *
comment
User comment to be added on line previous the cron job
commented

The cron job is set commented (prefixed with #DISABLED#). Defaults to False.

2016.3.0 新版功能.

identifier
Custom-defined identifier for tracking the cron line for future crontab edits. This defaults to the state id
special
A special keyword to specify periodicity (eg. @reboot, @hourly...)