crontab 示例

crontab是您希望定期运行的命令列表,也是用于管理该列表的命令的名称。crontab 代表“cron 表”,因为它使用作业调度程序 cron 来执行任务;cron本身以“chronos”(希腊语中的时间命名。cron 是系统进程,它将根据设定的时间表自动为您执行任务。时间表称为 crontab,这也是用于编辑该计划的程序的名称。

Linux Crontab Syntax

MIN HOUR DOM MON DOW CMD

Let’s break down each field:

 
 

Field

Description

Allowed Value

MIN (Minute)

Specifies the minute when the command will run

It ranges from 0 to 59.

HOUR

Denotes the hour of the day when the command is scheduled to execute.

It spans from 0 to 23.

DOM (Day of Month)

Specifies the day of the month for the task.

It ranges from 1 to 31.

MON (Month)

Indicates the month during which the command will be executed.

It varies from 1 to 12.

DOW (Day of Week)

Specifies the day of the week for the task.

It is represented by numbers from 0 to 6, where both 0 and 6 correspond to Sunday.

CMD (Command)

Represents the actual command or script that will run at the scheduled time.

—————–

 编辑 Crontab 条目编辑当前登录用户的 Crontab 条目。

要编辑 crontab 条目,请使用 crontab -e。默认情况下,这将编辑当前登录用户的 crontab。

使用 Cron 每分钟安排一项工作。

理想情况下,您可能不需要每分钟安排一次作业。但理解这个例子将有助于您理解其他例子。

* * * * * CMD

* 表示所有可能的单位 — 即全年每小时的每一分钟。除了直接使用这个 * 之外,您会发现它在以下情况下非常有用。当您在分钟字段中指定 */5 时,表示每 5 分钟。当您在分钟字段中指定 0-10/2 时,表示前 10 分钟内每 2 分钟。因此,上述约定可用于所有其他 4 个字段。

安排多次执行作业(例如,一天两次)

以下脚本每天进行两次增量备份。此示例每天 11:00 和 16:00 执行指定的增量备份 shell 脚本 (incremental-backup)。字段中以逗号分隔的值指定需要在上述所有时间内执行该命令。

00 11, 16 * * * /home/maverick/bin/incremental-backup

 

posted @ 2024-06-02 06:33  wongchaofan  阅读(7)  评论(0编辑  收藏  举报