centos

centos crontab时间格式怎么设置

小樊
51
2025-06-30 20:10:02
栏目: 智能运维

在 CentOS 系统中,使用 crontab 来设置定时任务时,时间的格式需要遵循特定的规则。crontab 的时间字段由五个部分组成,分别表示分钟、小时、日期、月份和星期几。每个字段的取值范围如下:

* * * * *  command_to_execute
- - - - -
| | | | |
| | | | ----- Day of the week (0 - 7) (Sunday to Saturday; 7 is also Sunday)
| | | ------- Month (1 - 12)
| | --------- Day of the month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

每个字段可以使用以下符号:

以下是一些示例:

  1. 每天凌晨 2 点执行命令:

    0 2 * * * /path/to/command
    
  2. 每周一到周五的上午 10 点执行命令:

    0 10 * * 1-5 /path/to/command
    
  3. 每月的 1 号和 15 号下午 3 点执行命令:

    0 15 1,15 * * /path/to/command
    
  4. 每小时的第 15 分钟执行命令:

    15 * * * * /path/to/command
    

要编辑 crontab 文件,可以使用以下命令:

crontab -e

编辑完成后保存并退出,新的定时任务将会生效。你可以使用 crontab -l 命令来查看当前用户的定时任务列表。

0
看了该问题的人还看了