Ubuntu 定时器(systemd timers)支持的时间格式主要包括以下几种:
相对时间
-
秒数:
1h表示 1 小时。30m表示 30 分钟。45s表示 45 秒。
-
分钟和小时:
1h30m表示 1 小时 30 分钟。2d表示 2 天。
-
天和周:
1w表示 1 周。2d3h表示 2 天 3 小时。
-
月和年:
1mo表示 1 个月。1y表示 1 年。
绝对时间
-
Unix 时间戳:
- 使用秒数表示的时间戳,例如
@1633072800表示自 1970-01-01 00:00:00 UTC 起的第 1633072800 秒。
- 使用秒数表示的时间戳,例如
-
特定日期和时间:
- 使用 ISO 8601 格式,例如
2021-10-01T12:00:00Z表示 UTC 时间 2021 年 10 月 1 日 12:00:00。
- 使用 ISO 8601 格式,例如
组合使用
可以组合使用相对时间和绝对时间来创建更复杂的定时任务。例如:
@reboot表示系统启动时执行。on-calendar=Sun *-*-* 08:00:00表示每周日的 8: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
通过这些时间格式,你可以灵活地设置定时任务,以满足不同的需求。