百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

定时任务新玩法!systemd timer 完整实战详解

nanshan 2025-05-26 17:44 19 浏览 0 评论

原文链接:「链接」

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=是要执行的主命令,即该服务实际执行的动作。



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 自动启动。





三、实战操作:每分钟执行 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。



2.添加执行权限

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


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




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



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



5.启用定时器

pdsyw@pdsyw-PC:~/Desktop$ sudo systemctl daemon-reload

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


用途:

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

启用定时器并立即启动。




6.验证定时器

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


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




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


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




pdsyw@pdsyw-PC:~/Desktop$ cat /var/log/hello_timer.log

Hello from systemd timer at 2025年 04月 17日 星期四 15:41:00 CST

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




四、进阶功能实战建议

下面这些是 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 分钟再执行,避免系统刚启动就跑定时任务造成负担。



  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秒,用于防止多个任务同时启动,提升系统稳定性。



  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。



  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 在主任务执行完后执行,用于清理、通知或记录任务完成。




五、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,你会发现新世界的大门已经为你打开!如果你觉得这篇文章对你有帮助,欢迎 分享点赞,点个在看和关注哦!我们下次再见!

相关推荐

微软发布Win11/10 ISO镜像Defender更新,提升系统初始安全性

IT之家7月27日消息,除了Setup及WinRE更新外,NeoWin发现微软本周还针对Windows11/10/Server安装镜像发布了新的Defender安全智能...

微软革新Windows装机体验:内置应用全面升级,安全与便捷双提升

Windows内置应用迎来重大变革:更安全、更快速的初始体验如果您曾亲自安装过Windows11,或许注意到其内置应用并非开箱即用,而是一些占位程序,需要首次运行时从微软应用商店(Microsoft...

Hotpatch继续扩展 现在更多Windows PC在更新后无需重启

Windows11最近从其服务器版本中获得了一项非常重要的功能:Windows热补丁。该功能旨在通过允许操作系统在无需重启的情况下安装重要的安全更新来最大限度地减少停机时间和中断。最初,微软在...

微软承认Windows Server六月更新存在BUG:导致DHCP服务器故障

IT之家6月17日消息,科技媒体WindowsLatest今天(6月17日)发布博文,报道称微软承认6月WindowsServer更新存在BUG,可能导致DHCP服...

Windows Server2019安装Hyper-V的2个简单方法!

关于WindowsServer2019WindowsServer2019是微软发布的服务器操作系统,是WindowsServer2016的后续版本。它包含了许多新的特性和改进,适用于数据中心...

如何在不满足系统要求的旧计算机上安装 Windows 11 24H2

如果你想了解这个安装工具以及安装方法(老飞摄影微信公众号内提供安装包下载),请完整的看完后面的文字,以避免在安装过程当中出现问题。Windows11通常需要某些硬件功能,例如TPM和安全启动,...

第 137 期:微软表示 Windows 11 24H2 是迄今为止最稳定的版本

就在刚刚,微软“大言不惭”地声称,Windows1124H2是迄今为止最可靠的Windows版本。我们并不是说它很糟糕,因为我们每天的工作中也在使用它。上述言论只是一份微软的一份官方文件的一...

Windows 11 将推出带有“高级”选项的新设置页面

Windows11即将迎来一个包含一些高级功能的全新“设置”页面。严格来说,它并非全新功能。它更像是“开发者”栏目的重新设计,用户和开发者可以在其中调整各种附加功能。微软可能明白这些东西不仅对开发...

Windows server 2025 重复数据删除

一、概述windowsserver中的重复数据删除功能从windowsserver2012就开始支持了。Windowsserver中默认没有安装重复数据删除功能。在磁盘分区(卷)上启用重复...

Windows Server 2025预览版迎来更新,微软改善Insiders测试体验

在发布WindowsServer的build26040版本之际,微软公布了该产品的官方名称:WindowsServer2025。一同推出的,还有Windows11WindowsInsid...

升不升?Win11 24H2大范围推送了

微软在其官方支持文档中宣布,24H2版现在已经开始向运行Windows11原始版本、22H2和23H2版的合格设备推送。Windows11的24H2更新现已进入新的可用性阶段,这意味着更多符合条件...

微软发布Win11/10/Server安装镜像Defender更新

IT之家6月22日消息,继上个月为Lumma发布更新后,微软本月也为Windows11/10/Server安装镜像发布了新的Defender更新。此更新包很有必要,因为Wi...

第 81 期:微软最近的更新给 Windows Server 带来了 DHCP 问题

近日,微软确认,DHCP服务器服务可能会在WindowsServer安装2025年6月更新后停止响应或拒绝连接。DHCP问题会影响WindowsServer2025(KB50...

windws server 2012 R2 虚拟机windows server2019 经常断网事件

故障现象:在windowsserver2012R2的虚拟主机上面搭建一个Windowsserver2019的虚拟机系统用来做域控。安装完设置好防火墙和IP,经过测试是可以ping同正常访问...

微软扩展热补丁部署,现覆盖ARM架构Win11 24H2设备

IT之家7月9日消息,科技媒体NeoWin今天(7月9日)发布博文,报道称微软扩大热补丁(WindowsHotpatching)覆盖范围,在AMD和英特尔处理器设备外,现覆盖支...

取消回复欢迎 发表评论: