面向 Linux 运维岗的系统性学习材料,结合本机(WSL + AlmaLinux 9)实战编写。
这篇写了什么
四个小节,从概念到能跑:先讲 timer 是什么、和 cron 差在哪;再讲 service 与 timer 两个文件怎么配对;然后是 OnCalendar 时间表达式;最后从零做一个每天 0 点执行的任务,末尾附常见问题排查。
快速上手(30 秒版)
目标:每天 0 点打印一条信息
# 1. 写脚本
sudo tee /usr/local/bin/printit.sh >/dev/null <<'EOF'
#!/bin/bash
echo "定时任务执行啦!当前时间:$(date '+%F %T')"
EOF
sudo chmod +x /usr/local/bin/printit.sh
# 2. 写 service 文件
sudo tee /etc/systemd/system/printit.service >/dev/null <<'EOF'
[Unit]
Description=每天打印任务
[Service]
Type=oneshot
ExecStart=/usr/local/bin/printit.sh
EOF
# 3. 写 timer 文件
sudo tee /etc/systemd/system/printit.timer >/dev/null <<'EOF'
[Unit]
Description=每天0点触发打印
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
EOF
# 4. 启用
sudo systemctl daemon-reload
sudo systemctl enable --now printit.timer
# 5. 手动跑一次 + 看日志
sudo systemctl start printit.service
sudo journalctl -u printit.service --since today
一句话原理
timer 只管什么时候触发 -> 到点启动同名 .service -> .service 里的 ExecStart 执行具体命令。
01 systemd timer 基础概念
一、它是什么?
systemd timer 是 systemd(Linux 的系统管理器/init 进程)提供的一种定时任务机制。
- systemd 自带功能,不需要装任何软件。
- 作用:让系统在指定时间点自动执行某个操作。
- 它是 crontab 的现代替代品。
二、和你机器的关系
系统是 AlmaLinux 9.8,systemd 252。查看版本:systemctl –version
本机已经在用 timer(dnf-makecache.timer、logrotate.timer):
systemctl list-timers --all
systemctl list-unit-files --type=timer
三、为什么比 crontab 更受运维青睐?
| 对比 | crontab | systemd timer |
|---|---|---|
| 额外 daemon | 依赖 crond 常驻 | 由 systemd 自己管 |
| 日志 | 默认发 mail,易丢 | 自动进 journald |
| 精度 | 分钟级 | 秒级,还可 OnBoot/OnActive |
| 错过补跑 | 无 | Persistent=true 可补 |
| 依赖控制 | 无 | 可 After=/Wants= |
| 资源限制 | 无 | MemoryMax/CPUQuota |
| 执行环境 | PATH 几乎全空 | 可设 PATH/WorkingDirectory |
四、两个单元配合
| 文件 | 作用 | 类比 |
|---|---|---|
| xxx.service | 做什么(要执行的命令/脚本) | crontab 的命令列 |
| xxx.timer | 什么时候做 | crontab 的时间列 |
timer 到点后启动同名 service。名字必须匹配。
五、重要提醒
- timer 和 git 无关,ExecStart 可放任意命令(echo/python/脚本/git push…)。
- 存放路径:系统级 /etc/systemd/system,用户级 ~/.config/systemd/user。
- 改配置后一定要 systemctl daemon-reload。
- systemctl start xxx.timer 是开始计时,不是立即执行;立即执行用 systemctl start xxx.service。
02 timer 与 service 配合
最小结构
/etc/systemd/system/
├── mytask.service -> 要执行什么
└── mytask.timer -> 什么时候触发
service 文件
[Unit]
Description=我的定时任务
[Service]
Type=oneshot
ExecStart=/usr/bin/echo hello
| 配置 | 含义 | 示例 |
|---|---|---|
| Type | oneshot=执行一次就结束 | Type=oneshot |
| ExecStart | 要执行的命令 | ExecStart=/opt/script.sh |
| WorkingDirectory | 工作目录 | WorkingDirectory=/root/scripts |
| User/Group | 以谁运行 | User=backup |
| Environment | 环境变量/PATH | Environment=PATH=/usr/local/bin:/usr/bin |
| MemoryMax | 内存上限 | MemoryMax=512M |
| CPUQuota | CPU 配额 | CPUQuota=50% |
timer 文件
[Unit]
Description=每天触发
[Timer]
OnCalendar=*-*-* 00:00:00
Persistent=true
Unit=mytask.service
[Install]
WantedBy=timers.target
触发方式不止 OnCalendar:
- OnBootSec=5min(开机后 5 分钟)
- OnUnitActiveSec=1h(上次运行后隔 1 小时)
- 可同时写多个,都生效。
常用命令
systemctl daemon-reload
systemctl enable --now mytask.timer
systemctl start mytask.service # 立即手动跑一次
systemctl status mytask.timer
systemctl list-timers mytask.timer
journalctl -u mytask.service --since today
systemctl stop mytask.timer
systemctl disable mytask.timer
常见坑
- 改文件后忘记 daemon-reload。
- enable –now 只是开始计时,不是立刻执行。
- ExecStart 里的 $ 需要转义为
\$。 - 权限不一致会导致任务失败,看 journalctl。
03 OnCalendar 时间表达式手册
格式: 星期 月-日 时:分:秒
最常用写法
| 需求 | OnCalendar 语法 |
|---|---|
| 每分钟 | --* *:0/1:00 |
| 每小时整点 | --* *:00:00 |
| 每2小时 | --* *:0/2:00 |
| 每天00:00 | --* 00:00:00 |
| 每天02:30 | --* 02:30:00 |
| 每周一09:00 | Mon --* 09:00:00 |
| 每月1号 | --01 00:00:00 |
| 每5分钟 | --* *:0/5:00 |
当前你要的-每天0点执行
OnCalendar=*-*-* 00:00:00
或简写(推荐):
OnCalendar=daily
daily = 每天 00:00。推荐直接写 daily,可读性更强。
用 systemd 验证表达式(不真执行,只查语法)
systemd-analyze calendar "daily"
systemd-analyze calendar "*-*-* 00:00:00"
会显示最近几次和下一次触发时间。
提示
- 写对了,systemctl list-timers 会显示 NEXT 时间。
- 想立刻试,用 systemctl start
手动跑。 - 时区用 timedatectl 查看。
04 完整实战:每天 0 点打印一条信息
目标
让系统每天 00:00 自动打印一条信息。
Step 1:写脚本 /usr/local/bin/printit.sh
#!/bin/bash
echo "每天0点执行 当前时间:$(date '+%F %T')"
执行权限:sudo chmod +x /usr/local/bin/printit.sh
Step 2:写 service /etc/systemd/system/printit.service
[Unit]
Description=每天打印任务
[Service]
Type=oneshot
ExecStart=/usr/local/bin/printit.sh
Step 3:写 timer /etc/systemd/system/printit.timer
[Unit]
Description=每天0点触发打印
[Timer]
OnCalendar=daily
Persistent=true
Unit=printit.service
[Install]
WantedBy=timers.target
Step 4:启用
sudo systemctl daemon-reload
sudo systemctl enable --now printit.timer
sudo systemctl list-timers printit.timer
会看到 NEXT = 明天 00:00。
Step 5:手动验证(不等 00:00)
sudo systemctl start printit.service
sudo journalctl -u printit.service --since today
小技巧
想快速验证逻辑,临时改成每分钟 OnCalendar=--* *:0/1:00,确认能跑再改回 daily。 改完记得 daemon-reload。
排错指南:systemd timer 常见问题
1. 配置改了没生效
改完 /etc/systemd/system 下的文件,必须: systemctl daemon-reload
2. timer 没跑 / 任务没执行
- 看状态: systemctl status
.timer - 看下次时间: systemctl list-timers
.timer - 若无 NEXT 时间说明表达式没写对或没 enable
- 手动跑一次测试: systemctl start
.service
3. 找不到 Unit
Unit xxx.service could not be found -> 名字写错、目录不对、或没 daemon-reload。 检查 /etc/systemd/system 下文件名与 Unit= 前缀一致。
4. 任务失败(看日志)
journalctl -u <name>.service --since today
常见原因:
- ExecStart 命令路径写错(用 which 确认路径)
- 权限不足(脚本没加执行权限: chmod +x)
- 脚本内 $ 需转义: ExecStart 里 $ 转成 $
5. Exec format / 脚本没有执行权限
chmod +x /path/script.sh
6. 校验时间表达式
systemd-analyze calendar "daily"
评论
留言
无需注册,留个名字即可。理性发言,彼此尊重。
正在载入留言…