ubuntu

ubuntu定时器支持哪些时间格式

小樊
48
2025-05-15 17:48:00
栏目: 智能运维

Ubuntu 定时器(systemd timers)支持的时间格式主要包括以下几种:

相对时间

  1. 秒数

    • 1h 表示 1 小时。
    • 30m 表示 30 分钟。
    • 45s 表示 45 秒。
  2. 分钟和小时

    • 1h30m 表示 1 小时 30 分钟。
    • 2d 表示 2 天。
  3. 天和周

    • 1w 表示 1 周。
    • 2d3h 表示 2 天 3 小时。
  4. 月和年

    • 1mo 表示 1 个月。
    • 1y 表示 1 年。

绝对时间

  1. Unix 时间戳

    • 使用秒数表示的时间戳,例如 @1633072800 表示自 1970-01-01 00:00:00 UTC 起的第 1633072800 秒。
  2. 特定日期和时间

    • 使用 ISO 8601 格式,例如 2021-10-01T12:00:00Z 表示 UTC 时间 2021 年 10 月 1 日 12:00:00。

组合使用

可以组合使用相对时间和绝对时间来创建更复杂的定时任务。例如:

示例

以下是一些常见的 systemd 定时器配置示例:

[Timer]
OnCalendar=Sun *-*-* 08:00:00
Persistent=true

[Install]
WantedBy=timers.target
[Timer]
OnBootSec=5min
OnUnitActiveSec=1h
Unit=my-service.service

[Install]
WantedBy=timers.target

通过这些时间格式,你可以灵活地设置定时任务,以满足不同的需求。

0
看了该问题的人还看了