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 秒。特定日期和时间:
2021-10-01T12:00:00Z
表示 UTC 时间 2021 年 10 月 1 日 12:00:00。可以组合使用相对时间和绝对时间来创建更复杂的定时任务。例如:
@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
通过这些时间格式,你可以灵活地设置定时任务,以满足不同的需求。