
systemd timer 是 Linux systemd 系统和服务管理器的一部分,它提供了一种现代化的方式来替代传统的 cron 任务调度。
你可以将 systemd timer 看作是“定时服务”,它可以在特定时间或按周期性地触发一个 systemd 服务(.service 单元)。
目前已经有不少的Linux服务已经使用systemd timer来管理定时任务,如下是我本地测试机的截图。

systemd timer 由两个单元文件组成:
.timer 文件:定义何时以及如何触发任务。.service 文件:定义实际要执行的任务。.service 文件# /etc/systemd/system/hello-timer.service
[Unit]
Description=Hello Timer Service
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'date >> /tmp/hello-timer.log'说明:
Type=oneshot:表示这个服务运行一次就结束。ExecStart:指定要执行的命令。.timer 文件# /etc/systemd/system/hello-timer.timer
[Unit]
Description=Run Hello Timer Every Minute
Requires=hello-timer.service
[Timer]
OnCalendar=*:*:00
AccuracySec=1s
Persistent=true
[Install]
WantedBy=timers.target说明:
OnCalendar=*:*:00:表示每分钟的第0秒触发(即每分钟一次)。AccuracySec=1s:高精度触发,避免延迟(默认是1分钟,可能延迟)。Persistent=true:即使系统在任务本该运行时处于关机状态,下次开机后会补执行(类似 cron 的 @reboot 行为扩展)。# 重新加载 systemd 配置
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
# 启用并启动 timer
sudo systemctl enable hello-timer.timer
sudo systemctl start hello-timer.timer# 查看 timer 状态
systemctl list-timers
# 或
systemctl status hello-timer.timer
# 查看日志
journalctl -u hello-timer.service -f
# 列出所有hello-timer的执行情况
systemctl list-timers --all | grep hello-timer相比传统 cron,systemd timer 有以下显著优势:
特性 | systemd timer | cron |
|---|---|---|
日历语法更强大 | 支持 weekly, monthly, hourly, daily, mon,thu, 2025-*-1 *:00:00 等 | 基本支持但不够灵活 |
高精度控制 | 可设置 AccuracySec=1ms 到纳秒级精度 | 通常最小粒度为1分钟 |
持久化任务 | Persistent=true 可在系统休眠/关机后补执行 | 不支持自动补执行 |
日志集成 | 自动记录到 journald,便于排查问题 | 需手动重定向日志 |
依赖管理 | 可设置依赖其他服务(如 After=network.target) | 无原生依赖支持 |
用户级定时器 | 支持用户会话级定时器(无需 root) | 需配置用户 crontab |
安全性 | 支持 sandboxing、权限控制等安全特性 | 权限控制较弱 |
状态查看 | systemctl list-timers 提供清晰视图 | crontab -l 仅显示配置 |
# backup-db.service
[Unit]
Description=Backup PostgreSQL Database
After=network.target
[Service]
Type=oneshot
User=postgres
ExecStart=/usr/bin/pg_dump mydb > /backups/mydb-$(date +%%Y%%m%%d).sql# backup-db.timer
[Unit]
Description=Daily Database Backup at 2:00 AM
[Timer]
OnCalendar=daily
OnCalendar=*-*-* 02:00:00
Persistent=true
[Install]
WantedBy=timers.target# weekly-reminder.service
[Unit]
Description=Send Weekly Reminder Email
[Service]
Type=oneshot
ExecStart=/usr/bin/mail -s "Weekly Reminder" user@example.com < /home/user/reminders.txt# weekly-reminder.timer
[Unit]
Description=Send Reminder Every Monday at 9 AM
[Timer]
OnCalendar=mon *-*-* 09:00:00
Persistent=true
[Install]
WantedBy=timers.target# init-script.service
[Unit]
Description=Run Initialization Script
[Service]
Type=oneshot
ExecStart=/opt/scripts/init.sh# init-script.timer
[Unit]
Description=Run Init Script 5 Minutes After Boot
[Timer]
OnBootSec=5min
# 或 OnStartupSec=5min(从 systemd 启动开始计时)
[Install]
WantedBy=timers.target# sync-data.timer
[Timer]
OnCalendar=hourly
AccuracySec=1s
Persistent=true等价于 00 * * * * 的 cron 表达式,但更语义化。
其它可选参数含义说明:
[Service]
Restart=on-failure
RestartSec=30
StartLimitIntervalSec=600
StartLimitBurst=5[Service]
MemoryLimit=128M
CPUQuota=20%写法 | 含义 |
|---|---|
*-*-* 03:00:00 | 每天3点整 |
hourly | 每小时一次 |
daily | 每天凌晨1:00(可配置) |
weekly | 每周一次 |
monthly | 每月一次 |
mon,fri *-*-* 18:00:00 | 每周一和周五晚上6点 |
*-01-01 00:00:00 | 每年元旦零点 |
Sat *-*-* 10:00:00 | 每周六上午10点 |
使用
systemd-analyze calendar "Mon *-*-* 09:00:00"可验证时间表达式是否正确。
systemd timer 是现代 Linux 系统中推荐的任务调度方式,尤其适合与系统服务集成的场景。它的优势在于:
systemd 生态无缝集成虽然对于简单的用户级任务,crontab 依然方便,但在服务器自动化、系统维护等领域,systemd timer 正在逐步取代 cron 成为主流选择。
如果你正在设计新的自动化任务或者希望做基础软件的交付,涉及到定时任务的话强烈建议优先考虑使用 systemd timer。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。