1. 首页 > Linux教程 > 正文

Linux教程FG398-定时任务与调度

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kuberne学习交流加群风哥QQ113257174tes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

风哥提示:

本文档介绍定时任务与调度的配置方法。

Part01-Cron定时任务

1.1 Cron基础配置

# 查看cron服务状态
[root@linux ~]# systemctl status crond
● crond.service – Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; preset: enabled)
Active: active (running) since Fri 2026-04-04 00:00:00 CST; 1h ago
Main PID: 1234 (crond)
Tasks: 1 (limit: 11232)
Memory: 2.0M
CGroup: /system.slice/crond.service
└─1234 /usr/sbin/crond -n

# 查看当前用户的定时任务
[root@linux ~]# crontab -l
no crontab for root

# 编辑定时任务
[root@linux ~]# crontab -e

# 添加定时任务
# 格式: 分 时 日 月 周 命令
# 每天凌晨2点执行备份
0 2 * * * /fglinux/scripts/backup.sh >> /var/log/backup.log 2>&1

# 每小时执行一次检查
0 * * * * /fglinux/scripts/health_check.sh

# 每5分钟执行一次监控
*/5 * * * * /fglinux/scripts/monitor.sh

# 每周一凌晨3点执行清理
0 3 * * 1 /fglinux/scripts/cleanup.sh

# 每月1号凌晨4点执行报告
0 4 1 * * /fglinux/scripts/monthly_report.sh

# 查看添加后的任务
[root@linux ~]# crontab -l
0 2 * * * /fglinux/scripts/backup.sh >> /var/log/backup.log 2>&1
0 * * * * /fglinux/scripts/health_check.sh
*/5 * * * * /fglinux/scripts/monitor.sh
0 3 * * 1 /fglinux/scripts/cleanup.sh
0 4 1 * * /fglinux/scripts/monthly_report.sh

# 查看cron日志
[root@linux ~]# tail -f /var/log/cron
Apr 4 02:00:00 linux CROND[12345]: (root) CMD (/fglinux/scripts/backup.sh >> /var/log/backup.log 2>&1)
Apr 4 02:00:00 linux CROND[12346]: (root) MAIL (mailed 123 bytes of output but got status 0x004b#012)

1.2 系统级定时任务

# 系统级定时任务目录
[root@linux ~]# ls -la /etc/cron.d/
total 20
drwxr-xr-x. 2 root root 4096 Apr 4 00:00 .
drwxr-xr-x. 95 root root 8192 Apr 4 00:00 ..
-rw-r–r–. 1 root root 128 Apr 4 00:00 0hourly
-rw-r–r–. 1 root root 235 Apr 4 00:00 sysstat

# 创建系统级定时任务
[root@linux ~]# cat > /etc/cron.d/fgedu-backup << 'EOF' # fgedu backup task SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # 每天凌晨2点执行备份 0 2 * * * root /fglinux/scripts/backup.sh >> /var/log/fgedu-backup.log 2>&1
EOF

# 查看定时任务目录
[root@linux ~]# ls -la /etc/cron.*/
/etc/cron.d/:
total 20
drwxr-xr-x. 2 root root 4096 Apr 4 00:00 .
drwxr-xr-x. 95 root root 8192 Apr 4 00:00 ..
-rw-r–r–. 1 root root 128 Apr 4 00:00 0hourly
-rw-r–r–. 1 root root 235 Apr 4 00:00 sysstat
-rw-r–r–. 1 root root 113 Apr 4 00:00 fgedu-backup

/etc/cron.daily/:
total 8
drwxr-xr-x. 2 root root 4096 Apr 4 00:00 .
drwxr-xr-x. 95 root root 8192 Apr 4 00:00 ..
-rwx——. 1 root root 219 Apr 4 00:00 logrotate

/etc/cron.hourly/:
total 8
drwxr-xr-x. 2 root root 4096 Apr 4 00:00 .
drwxr-xr-x. 95 root root 8192 Apr 4 00:00 ..
-rwx——. 1 root root 193 Apr 4 00:00 0anacron

/etc/cron.monthly/:
total 0
drwxr-xr-x. 2 root root 4096 Apr 4 00:00 .
drwxr-xr-x. 95 root root 8192 Apr 4 00:00 ..

/etc/cron.weekly/:
total 0
drwxr-xr-x. 2 root root 4096 Apr 4 00:00 .
drwxr-xr-x. 95 root root 8192 Apr 4 00:00 ..

# 添加每日执行任务
[root@linux ~]# cat > /etc/cron.daily/fgedu-check << 'EOF' #!/bin/bash # fgedu-check.sh # from:www.itpux.com.qq113257174.wx:itpux-com # web: http://www.fgedu.net.cn echo "$(date): 开始系统检查" # 检查磁盘空间 df -h | grep -E '^/dev' | while read line; do usage=$(echo $line | awk '{print $5}' | tr -d '%') if [ $usage -gt 80 ]; then echo "警告: $line" fi done # 检查内存 free -m | grep Mem # 检查负载 uptime echo "$(date): 检查完成" EOF [root@linux ~]# chmod +x /etc/cron.daily/fgedu-check

Part02-Systemd定时器

2.1 Systemd Timer配置

# 创建服务单元
[root@linux ~]# cat > /etc/systemd/system/fgedu-backup.service << 'EOF' [Unit] Description=FGEDU Backup Service After=network.tafrom PG视频:www.itpux.comrget [Service] Type=oneshot ExecStart=/fglinux/scripts/backup.sh User=root [Install] WantedBy=multi-user.target EOF # 创建定时器单元 [root@linux ~]# cat > /etc/systemd/system/fgedu-backup.timer << 'EOF' [Unit] Description=FGEDU Backup Timer Requires=fgedu-backup.service [Timer] OnCalendar=*-*-* 02:00:00 Persistent=true [Install] WantedBy=timers.target EOF # 重载systemd [root@linux ~]# systemctl daemon-reload # 启用并启动定时器 [root@linux ~]# systemctl enable --now fgedu-backup.timer Created symlink /etc/systemd/system/timers.target.wants/fgedu-backup.timer → /etc/systemd/system/fgedu-backup.timer. # 查看定时器列表 [root@linux ~]# systemctl list-timers NEXT LEFT LAST PASSED UNIT ACTIVATES Fri 2026-04-05 02:00:00 CST 23h left Fri 2026-04-04 02:00:00 CST 1h ago fgedu-backup.timer fgedu-backup.service Fri 2026-04-04 03:00:00 CST 1h 30min left Fri 2026-04-03 03:00:00 CST 23h ago logrotate.timer logrotate.service 2 timers listed. Pass --all to see loaded but inactive timers, too. # 查看定时器状态 [root@linux ~]# systemctl status fgedu-backup.timer ● fgedu-backup.timer - FGEDU Backup Timer Loaded: loaded (/etc/systemd/system/fgedu-backup.timer; enabled; preset: disabled) Active: active (waiting) since Fri 20学习交流加群风哥微信: itpux-com26-04-04 00:00:00 CST; 1h ago Trigger: Fri 2026-04-05 02:00:00 CST; 23h left Triggers: ● fgedu-backup.service # 手动触发执行 [root@linux ~]# systemctl start fgedu-backup.service # 查看执行日志 [root@linux ~]# journalctl -u fgedu-backup.service -- Logs begin at Fri 2026-04-01 00:00:00 CST. -- Apr 04 02:00:00 linux systemd[1]: Starting FGEDU Backup Service... Apr 04 02:00:05 linux backup.sh[12345]: 开始备份... Apr 04 02:00:10 linux backup.sh[12345]: 备份完成 Apr 04 02:00:10 linux systemd[1]: fgedu-backup.service: Succeeded. Apr 04 02:00:10 linux systemd[1]: Finished FGEDU Backup Service.
风哥针对定时任务建议:

  • 使用绝对路径执行脚本
  • 重定向输出到日志文件
  • 添加错误处理机制
  • 使用锁文件防止重复执行
  • 定期检查任务执行状态

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息