您的位置:首页 > 科技 > 能源 > 深圳如何搭建建网站_官方网站下载qq音速_qq营销_建个网站需要多少钱

深圳如何搭建建网站_官方网站下载qq音速_qq营销_建个网站需要多少钱

2025/4/21 14:29:08 来源:https://blog.csdn.net/m0_69493559/article/details/147308758  浏览:    关键词:深圳如何搭建建网站_官方网站下载qq音速_qq营销_建个网站需要多少钱
深圳如何搭建建网站_官方网站下载qq音速_qq营销_建个网站需要多少钱

原文链接:定时任务新玩法!systemd timer 完整实战详解

Hello,大家好啊!今天给大家带来一篇使用 systemd timer 实现定时任务调度的详细实战文章。相比传统的 crontab,systemd timer 更加现代化、结构清晰、支持日志记录、失败处理、启动延迟等功能,是信创终端操作系统和现代 Linux 系统中首选的定时任务方案!如果你也想实现以下目标:

精准定时任务

服务级别的控制

可追踪的运行日志

延迟、随机、自动补偿等智能调度

那请认真读完这篇文章!欢迎大家点赞、在看、关注哦!

一、什么是 systemd timer?

systemd timer 是基于 systemd 的系统级调度功能,用 .timer + .service 文件组合替代 cron,可以更加灵活地设置周期、时间点、启动延迟、失败重试等功能,尤其适合复杂调度和服务任务。

二、基础构成:.service + .timer

1..service 文件:定义要执行的任务

用途:创建一个 systemd 服务文件,定义要执行的任务内容(比如运行一个脚本)。

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /etc/systemd/system/hello.service
​
pdsyw@pdsyw-PC:~/Desktop$ cat /etc/systemd/system/hello.service
​
[Unit]
​
Description=打印当前时间日志
​​
[Service]
​
Type=oneshot
​
ExecStart=/usr/local/bin/hello.sh

用途:

Description=服务的说明文字,会出现在 systemctl status 中。

Type=oneshot 表示任务是一次性执行(脚本跑完即结束),适用于定时任务。

ExecStart=是要执行的主命令,即该服务实际执行的动作。

img

2..timer 文件:定义时间触发规则

用途:定义任务的执行时间计划,替代 crontab。

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /etc/systemd/system/hello.timer
​
pdsyw@pdsyw-PC:~/Desktop$ cat /etc/systemd/system/hello.timer
​
[Unit]
​
Description=每分钟执行一次 hello.sh
​​
[Timer]
​
OnCalendar=*-*-* *:*:00
​
Persistent=true
​​
[Install]
​
WantedBy=timers.target

用途:

Description=定时器说明,用于可读性。

OnCalendar=--* ::00 表示 每分钟的第0秒执行一次。

Persistent=true 表示如果机器宕机、关机错过了定时,开机时会自动补执行。

WantedBy=t让此定时器可以通过 systemctl enable 自动启动。

img

三、实战操作:每分钟执行 hello.sh 脚本

1.创建脚本文件

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /usr/local/bin/hello.sh
​
pdsyw@pdsyw-PC:~/Desktop$ cat /usr/local/bin/hello.sh
​
\#!/bin/bash
​
echo "Hello from systemd timer at $(date)" >> /var/log/hello_timer.log

用途:该脚本每次被调用时,记录当前时间到日志文件 /var/log/hello_timer.log。

img

2.添加执行权限

pdsyw@pdsyw-PC:~/Desktop$ sudo chmod +x /usr/local/bin/hello.sh

用途:赋予脚本执行权限,否则 systemd 执行会失败。

img

3.创建服务单元

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /etc/systemd/system/hello.service
​
pdsyw@pdsyw-PC:~/Desktop$ cat /etc/systemd/system/hello.service
​
[Unit]
​
Description=打印当前时间日志
​​
[Service]
​
Type=oneshot
​
ExecStart=/usr/local/bin/hello.sh

img

4.创建定时器单元

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /etc/systemd/system/hello.timer
​
pdsyw@pdsyw-PC:~/Desktop$ cat /etc/systemd/system/hello.timer
​
[Unit]
​
Description=每分钟执行一次 hello.sh
​​
[Timer]
​
OnCalendar=*-*-* *:*:00
​
Persistent=true
​​
[Install]
​
WantedBy=timers.target

img

5.启用定时器

pdsyw@pdsyw-PC:~/Desktop$ sudo systemctl daemon-reload
​
pdsyw@pdsyw-PC:~/Desktop$ sudo systemctl enable --now hello.timer

用途:

让 systemd 重新加载配置文件,必须执行!

启用定时器并立即启动。

img

6.验证定时器

pdsyw@pdsyw-PC:~/Desktop$ systemctl list-timers

用途:列出所有激活中的定时器,检查 hello.timer 是否已启用。

img

pdsyw@pdsyw-PC:~/Desktop$ sudo journalctl -u hello.service

用途:列出所有激活中的定时器,检查 hello.timer 是否已启用。

img

pdsyw@pdsyw-PC:~/Desktop$ cat /var/log/hello_timer.log
​
Hello from systemd timer at 2025年 04月 17日 星期四 15:41:00 CST
​

用途:查看脚本输出的内容是否正常写入日志文件。

img

四、进阶功能实战建议

下面这些是 systemd timer 真正比 cron 强大的地方!

  1. 延迟启动任务(OnBootSec)

有些服务不能系统一启动就运行,适合延迟加载:

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /etc/systemd/system/hello.timer
​
pdsyw@pdsyw-PC:~/Desktop$ cat /etc/systemd/system/hello.timer
​
[Unit]
​
Description=每分钟执行一次 hello.sh
​​
[Timer]
​
OnBootSec=10min
​
OnCalendar=*-*-* *:*:00
​
Persistent=true
​​
[Install]
​
WantedBy=timers.target

用途:系统开机后延迟 10 分钟再执行,避免系统刚启动就跑定时任务造成负担。

img

  1. 设置随机执行时间(RandomizedDelaySec)

防止多个定时器同时启动造成系统压力:

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /etc/systemd/system/hello.timer
​
pdsyw@pdsyw-PC:~/Desktop$ cat /etc/systemd/system/hello.timer
​
[Unit]
​
Description=每小时执行一次 hello.sh
​​
[Timer]
​
OnCalendar=*-*-* *:00:00
​
RandomizedDelaySec=30s
​
Persistent=true
​​
[Install]
​
WantedBy=timers.target

用途:在定时任务的时间点上随机延迟最多30秒,用于防止多个任务同时启动,提升系统稳定性。

img

  1. 控制触发精度(AccuracySec)

设置系统触发定时器的“精度窗口”,减少频繁触发:

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /etc/systemd/system/hello.timer
​
pdsyw@pdsyw-PC:~/Desktop$ cat /etc/systemd/system/hello.timer
​
[Unit]
​
Description=每小时执行一次 hello.sh
​​
[Timer]
​
AccuracySec=1s
​
OnCalendar=*-*-* *:00:00
​
RandomizedDelaySec=30s
​
Persistent=true
​​
[Install]
​
WantedBy=timers.target

用途:设置触发的时间精度窗口。例如 1s 表示尽可能在设定时间精确触发,默认是60s。

img

  1. 执行前置/后置逻辑(ExecStartPre / ExecStartPost)

结合 service 文件实现任务执行前后附加操作:

pdsyw@pdsyw-PC:~/Desktop$ sudo vim /etc/systemd/system/hello.service 
​
pdsyw@pdsyw-PC:~/Desktop$ cat /etc/systemd/system/hello.service 
​
[Unit]
​
Description=打印当前时间日志
​​
[Service]
​
Type=oneshot
​
ExecStartPre=/usr/bin/logger "任务开始"
​
ExecStart=/usr/local/bin/hello.sh
​
ExecStartPost=/usr/bin/logger "任务已完成"

用途:

ExecStartPre 在主任务开始之前执行,用于做准备工作或记录日志。

ExecStartPost 在主任务执行完后执行,用于清理、通知或记录任务完成。

img

img

五、OnCalendar 表达式速查

*表达式**含义*
--* ::00每分钟
--* *:00:00每小时
--1 00:00:00每月 1 日
Mon 08:00:00每周一早上 8 点
2024-12-31 23:59:00指定日期时间执行一次

六、常见问题与排查

*问题**排查建议*
定时器未触发systemctl list-timers 查看是否启用
没有日志记录检查 service 是否配置正确
脚本不执行添加执行权限 / 检查脚本路径
修改无效修改完需重新 daemon-reload 和启用

通过 systemd timer,你可以实现比 cron 更稳定、更灵活的定时任务调度机制,配合服务日志、失败处理、启动延迟等功能,是 信创终端操作系统 中现代化运维的首选。它适合:自动备份 / 自动同步

监控脚本定期运行

自动清理、日志轮转

延迟执行任务 / 周期调度

如果你还在用 cron,不妨试试 systemd timer,你会发现新世界的大门已经为你打开!如果你觉得这篇文章对你有帮助,欢迎 分享点赞,点个在看和关注哦!我们下次再见!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com