Skip to content

crontab Operations

Facts used in these operations: crontab.Crontab.

crontab.crontab

Add/remove/update crontab entries.

crontab.crontab(
         command: 'str',
         present=True,
         user: 'str | None' = None,
         cron_name: 'str | None' = None,
         minute: 'list[str | int] | str | int | None' = None,
         hour: 'list[str | int] | str | int | None' = None,
         month: 'list[str | int] | str | int | None' = None,
         day_of_week: 'list[str | int] | str | int | None' = None,
         day_of_month: 'list[str | int] | str | int | None' = None,
         special_time: 'str | None' = None,
         interpolate_variables=False,
         **kwargs,
    )
  • command: the command for the cron
  • present: whether this cron command should exist
  • user: the user whose crontab to manage
  • cron_name: name the cronjob so future changes to the command will overwrite
  • modify_cron_name: modify the cron name
  • minute: which minutes to execute the cron
  • hour: which hours to execute the cron
  • month: which months to execute the cron
  • day_of_week: which day of the week to execute the cron
  • day_of_month: which day of the month to execute the cron
  • special_time: cron "nickname" time (@reboot, @daily, etc), overrides others
  • interpolate_variables: whether to interpolate variables in command

Cron commands: Unless name is specified the command is used to identify crontab entries. This means commands must be unique within a given users crontab. If you require multiple identical commands, provide a different name argument for each.

Cron schedule: The values for minute, hour, month, day_of_week, day_of_month can be specified as an integer, a string containing a cron schedule or a list of integers or strings. The effective default value of all these arguments is "*" when special_time is not set.

Special times: When provided, special_time will be used instead of any values passed in for minute/hour/month/day_of_week/day_of_month.

Examples:

from pyinfra.operations import crontab

# simple example for a crontab
crontab.crontab(
    name="Backup /etc weekly",
    command="/bin/tar cf /tmp/etc_bup.tar /etc",
    cron_name="backup_etc",
    day_of_week=0,
    hour=1,
    minute=0,
)

# execute every five minutes
crontab.crontab(
    name="A harmless monitoring example",
    command="/usr/bin/ping 127.0.0.1",
    minute="*/5"
)

# execute on reboot
crontab.crontab(
    name="Create a directory on reboot",
    command="/usr/bin/mkdir /var/run/my_directory",
    special_time="@reboot"
)

Global arguments

This operation also inherits all global arguments.