● update-notifier-motd.timer - Check to see whether there is a new version of Ubuntu available Loaded: loaded (/lib/systemd/system/update-notifier-motd.timer; enabled; vendor preset: enabled) Active: active (waiting) since Tue 2023-04-04 16:49:50 CST; 19s ago Trigger: Sat 2023-04-08 03:19:02 CST; 3 days left Triggers: ● update-notifier-motd.service
4 月 04 16:49:50 casey-Virtual-Machine systemd[1]: Started Check to see whether there is a new version of Ubuntu available.
● fstrim.timer - Discard unused blocks once a week Loaded: loaded (/lib/systemd/system/fstrim.timer; enabled; vendor preset: enabled) Active: active (waiting) since Tue 2023-04-04 16:49:49 CST; 19s ago Trigger: Tue 2023-04-04 17:58:23 CST; 1h 8min left Triggers: ● fstrim.service Docs: man:fstrim
4 月 04 16:49:49 casey-Virtual-Machine systemd[1]: Started Discard unused blocks once a week. ...
每个 timer 至少有六行信息与之相关:
第一行是 timer 的文件名和对其用途的简短描述。
第二行显示 timer 的状态,它是否被加载,timer unit 文件的完整路径,以及供应商的预设。
第三行显示其活动状态,包括 timer 开始活动的日期和时间。
第四行包含 timer 下次被触发的日期和时间,以及直到触发发生的 大致 时间。
第五行显示由 timer 触发的事件或服务的名称。
一些(但不是全部)systemd unit 文件有指向相关文档的指针。如上面的 Docs: man:fstrim
最后一行是 timer 所触发的服务的最新实例的日志条目。
创建 timer
优势之一:统一日志收集到 systemd 日志
为了更快了解 timer, 我们创建自己的 service unit 和 timer unit 来触发。
$ systemd-analyze calendar 2030-06-17 Original form: 2030-06-17 Normalized form: 2030-06-17 00:00:00 Next elapse: Mon 2030-06-17 00:00:00 CST (in UTC): Sun 2030-06-16 16:00:00 UTC From now: 7 years 2 months left
现在添加一个时间。在这个例子中,日期和时间作为非相关实体被单独分析:
1 2 3 4 5 6 7 8 9 10 11 12
$ systemd-analyze calendar 2030-06-17 15:21:16 Original form: 2030-06-17 Normalized form: 2030-06-17 00:00:00 Next elapse: Mon 2030-06-17 00:00:00 CST (in UTC): Sun 2030-06-16 16:00:00 UTC From now: 7 years 2 months left
Original form: 15:21:16 Normalized form: *-*-* 15:21:16 Next elapse: Wed 2023-04-05 15:21:16 CST (in UTC): Wed 2023-04-05 07:21:16 UTC From now: 21h left
要把日期和时间作为一个 unit 来分析,需要用引号把它们括起来。
1 2 3 4 5
$ systemd-analyze calendar "2030-06-17 15:21:16" Normalized form: 2030-06-17 15:21:16 Next elapse: Mon 2030-06-17 15:21:16 CST (in UTC): Mon 2030-06-17 07:21:16 UTC From now: 7 years 2 months left
现在测试表 2 中的条目。选一个复杂的:
1 2 3 4 5 6
$ systemd-analyze calendar "2023-6,7,8-1,15 01:15:00" Original form: 2023-6,7,8-1,15 01:15:00 Normalized form: 2023-06,07,08-01,15 01:15:00 Next elapse: Thu 2023-06-01 01:15:00 CST (in UTC): Wed 2023-05-31 17:15:00 UTC From now: 1 month 26 days left
让我们看一个例子,在这个例子中,我们列出了时间戳表达式的下五个执行时间:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$ systemd-analyze calendar --iterations=5 "Mon *-05~3" Original form: Mon *-05~3 Normalized form: Mon *-05~03 00:00:00 Next elapse: Mon 2023-05-29 00:00:00 CST (in UTC): Sun 2023-05-28 16:00:00 UTC From now: 1 month 23 days left Iter. #2: Mon 2028-05-29 00:00:00 CST (in UTC): Sun 2028-05-28 16:00:00 UTC From now: 5 years 1 month left Iter. #3: Mon 2034-05-29 00:00:00 CST (in UTC): Sun 2034-05-28 16:00:00 UTC From now: 11 years 1 month left Iter. #4: Mon 2045-05-29 00:00:00 CST (in UTC): Sun 2045-05-28 16:00:00 UTC From now: 22 years 1 month left Iter. #5: Mon 2051-05-29 00:00:00 CST (in UTC): Sun 2051-05-28 16:00:00 UTC From now: 28 years 1 month left