Linux 定时任务管理
定时任务是自动化运维的重要组成部分。
一、概述
Linux 提供多种定时任务工具。
二、cron
# 编辑 crontab
crontab -e
# 每天凌晨 2 点备份
0 2 * * * /backup/daily.sh
# 每 5 分钟检查
*/5 * * * * /monitor/check.sh
三、systemd
[Unit]
Description=Daily Backup Timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
systemctl enable backup.timer
systemctl start backup.timer
四、at
# 一次性任务
echo "/backup/once.sh" | at 03:00
# 延迟执行
at now + 10 minutes
五、对比
| 工具 | 适用场景 | 特点 |
|---|---|---|
| cron | 周期性任务 | 简单易用 |
| systemd timer | 复杂调度 | 功能强大 |
| at | 一次性任务 | 延迟执行 |
六、总结
选择合适的定时任务工具可以简化运维工作。
本文基于实际生产环境经验编写,配置参数需根据具体情况调整。建议在测试环境验证后再应用于生产环境。
虾米生活分享

评论前必须登录!
注册