Linux 定时任务:crontab、anacron 与systemd timer

定时任务「漏跑、跑两次、时区错乱」,常见于 cron 与 systemd timer 混用、休眠机器、UTC/本地时混淆。本文对照三类机制,给出选择与排障路径。

源码锚点

路径 / 手册 作用
man crontab 用户任务表
man cron / /etc/cron.* 系统 cron 目录
man anacron 非 7×24 主机补跑
man systemd.timer timer 单元
man systemd.time 日历与单调时间表达式

timer 示例:

ini 复制代码
[Unit]
Description=Run backup daily

[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true

[Install]
WantedBy=timers.target

配套 backup.service 写实际 ExecStart=

调用链

#mermaid-svg-6IHOeQT8Ya4y1VHJ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .error-icon{fill:#552222;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .marker.cross{stroke:#333333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-6IHOeQT8Ya4y1VHJ p{margin:0;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .cluster-label text{fill:#333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .cluster-label span{color:#333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .cluster-label span p{background-color:transparent;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .label text,#mermaid-svg-6IHOeQT8Ya4y1VHJ span{fill:#333;color:#333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .node rect,#mermaid-svg-6IHOeQT8Ya4y1VHJ .node circle,#mermaid-svg-6IHOeQT8Ya4y1VHJ .node ellipse,#mermaid-svg-6IHOeQT8Ya4y1VHJ .node polygon,#mermaid-svg-6IHOeQT8Ya4y1VHJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .rough-node .label text,#mermaid-svg-6IHOeQT8Ya4y1VHJ .node .label text,#mermaid-svg-6IHOeQT8Ya4y1VHJ .image-shape .label,#mermaid-svg-6IHOeQT8Ya4y1VHJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .rough-node .label,#mermaid-svg-6IHOeQT8Ya4y1VHJ .node .label,#mermaid-svg-6IHOeQT8Ya4y1VHJ .image-shape .label,#mermaid-svg-6IHOeQT8Ya4y1VHJ .icon-shape .label{text-align:center;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .node.clickable{cursor:pointer;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .arrowheadPath{fill:#333333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-6IHOeQT8Ya4y1VHJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6IHOeQT8Ya4y1VHJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-6IHOeQT8Ya4y1VHJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .cluster text{fill:#333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .cluster span{color:#333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-6IHOeQT8Ya4y1VHJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .icon-shape,#mermaid-svg-6IHOeQT8Ya4y1VHJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .icon-shape p,#mermaid-svg-6IHOeQT8Ya4y1VHJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .icon-shape .label rect,#mermaid-svg-6IHOeQT8Ya4y1VHJ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6IHOeQT8Ya4y1VHJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-6IHOeQT8Ya4y1VHJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-6IHOeQT8Ya4y1VHJ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} cron
timer
anacron
时间到
机制
crond 读 crontab
systemd 触发 service
开机后补跑窗口
shell 跑命令
邮件/日志/自己重定向

重点知识

crontab 要点

bash 复制代码
crontab -e
# m h dom mon dow command
grep CRON /var/log/syslog 2>/dev/null | tail
journalctl -u cron -b | tail

默认环境变量极少(常只有 SHELL/PATH 子集)。脚本应用绝对路径,输出重定向到日志。

systemd timer 优点

  • 依赖、资源限制、沙箱可复用 service 能力。
  • systemctl list-timers 一眼看下次触发。
  • Persistent=true 利于休眠后补跑。
bash 复制代码
systemctl list-timers --all
systemctl status backup.timer backup.service

何时用谁

场景 更合适
用户级简单脚本 crontab
要可靠依赖/日志/权限 systemd timer
桌面/笔记本非全天开机 anacron 或 Persistent timer

常见坑

  • 夏令时/时区:timedatectl 先确认。
  • cron 里 % 有特殊含义,需转义。
  • 同时装 timer 与 crontab 跑同一任务 → 双份执行。
相关推荐
IT摆渡者1 小时前
1panel 配置申请手动解析网站SSL证书
服务器·网络协议·ssl
mounter6251 小时前
Linux 7.2 引入更灵活的块设备错误注入机制
linux·linux kernel·kernel·disk·error injection
疯狂打码的少年2 小时前
【计算机网络】子网掩码与子网划分计算(含CIDR表示法)
服务器·笔记·计算机网络·php
cpolar技术支持2 小时前
Docker 容器启动失败怎么查?端口、日志、权限与网络排障完整教程
linux·docker·容器·cpolar·网络排障
网硕互联的小客服2 小时前
怎样在CMD里用chkntfs /x C: 命令来取消磁盘自检?——原理与实践全解
运维·服务器·网络
布莱克6052 小时前
Linux 常用命令详解:从入门到实战
linux·运维·服务器
朽棘不雕3 小时前
可执行程序的格式
linux
IT摆渡者4 小时前
11Panel 部署雷池WAF
linux
学习路上_write4 小时前
Linux黑马命令学习
linux·运维·服务器