惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

菜鸟教程

REST API 教程 | 菜鸟教程 Python 进度条 | 菜鸟教程 ChromeDriver 说明 | 菜鸟教程 pip 指定国内镜像,并设置不使用 https | 菜鸟教程 Trae 入门教程 | 菜鸟教程 Dify 零门槛打造专属 AI 应用 | 菜鸟教程 1.11 算法代码练习 | 菜鸟教程 解决 VS 编译中产生 C4996 错误的方式 中国大模型大全 | 菜鸟教程
Linux Cron 定时任务 | 菜鸟教程
tianqixin · 2025-05-29 · via 菜鸟教程

📋 Cron 时间格式

* * * * * 命令

分 时 日 月 周 要执行的命令

星期 (0-7)

每周的第几天执行
(0和7都表示周日)

🎯 特殊字符说明

  • * 表示任意值,匹配所有可能的值
  • , 表示列表,如 1,3,5 表示1点、3点、5点
  • - 表示范围,如 1-5 表示1到5
  • / 表示间隔,如 */5 表示每5个单位
  • ? 用于日期和星期字段,表示不指定值

🔄 每分钟执行

* * * * * /path/to/script.sh

每分钟都会执行一次,常用于测试

🕐 每小时执行

0 * * * * /path/to/script.sh

每小时的第0分钟执行,如 1:00, 2:00, 3:00

🌅 每天早上8点执行

0 8 * * * /path/to/script.sh

每天上午8:00执行,适合日常任务

🌙 每天深夜执行

0 2 * * * /path/to/backup.sh

每天凌晨2:00执行,适合备份任务

📅 每周一执行

0 9 * * 1 /path/to/weekly-report.sh

每周一上午9:00执行,适合周报生成

📆 每月1号执行

0 0 1 * * /path/to/monthly-task.sh

每月1号凌晨执行,适合月度任务

⚡ 每5分钟执行

*/5 * * * * /path/to/monitor.sh

每5分钟执行一次,适合监控脚本

🔄 每30分钟执行

*/30 * * * * /path/to/sync.sh

每30分钟执行一次,适合同步任务

💼 工作日每小时执行

0 * * * 1-5 /path/to/workday-task.sh

周一到周五每小时执行,适合办公自动化

🎯 特定时间点执行

30 9,14,18 * * * /path/to/reminder.sh

每天9:30、14:30、18:30执行

📈 工作时间内每15分钟

*/15 9-17 * * 1-5 /path/to/status-check.sh

工作日9-17点每15分钟执行

🌸 季度任务

0 0 1 1,4,7,10 * /path/to/quarterly.sh

每季度第一天执行(1月、4月、7月、10月)

🎉 年度任务

0 0 1 1 * /path/to/yearly-cleanup.sh

每年1月1日执行年度清理

⏰ 复杂时间组合

15,45 */2 * * * /path/to/complex-task.sh

每2小时的第15分钟和45分钟执行

🔧 带输出重定向

0 2 * * * /path/to/backup.sh >> /var/log/backup.log 2>&1

执行备份并记录日志,包含错误信息

🌐 环境变量设置

0 3 * * * cd /app && /usr/bin/python3 script.py

切换到指定目录后执行Python脚本

📊 数据库备份

0 2 * * * mysqldump -u root -p database_name > /backup/db_$(date +\%Y\%m\%d).sql

每天凌晨2点备份MySQL数据库

🧹 日志清理

0 1 * * 0 find /var/log -name "*.log" -mtime +30 -delete

每周日凌晨1点删除30天前的日志文件

🔄 Git 自动同步

*/10 * * * * cd /path/to/repo && git pull origin main

每10分钟自动拉取Git仓库更新

🌡️ 系统监控

*/5 * * * * top -bn1 | grep "Cpu(s)" >> /var/log/cpu_usage.log

每5分钟记录CPU使用率

🔒 SSL证书检查

0 9 * * * openssl x509 -in /etc/ssl/cert.pem -noout -checkend 2592000

每天检查SSL证书是否即将过期

📱 API健康检查

*/2 * * * * curl -f http://localhost:8080/health || echo "API Down" | mail -s "Alert" [email protected]

每2分钟检查API状态,异常时发邮件

🔄 Docker容器重启

0 4 * * * docker restart $(docker ps -q --filter "status=exited")

每天凌晨4点重启已停止的Docker容器

📝 Cron管理命令

crontab -e # 编辑当前用户的crontab
crontab -l # 查看当前crontab
crontab -r # 删除当前crontab

基本的crontab管理命令

👤 用户级别管理

sudo crontab -u username -e # 编辑指定用户的crontab
sudo crontab -u username -l # 查看指定用户的crontab

管理其他用户的定时任务

📊 日志记录最佳实践

0 2 * * * /path/to/script.sh >> /var/log/cron_script.log 2>&1

将标准输出和错误都重定向到日志文件

🔧 环境变量设置

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 2 * * * /path/to/script.sh

在crontab中设置环境变量

⚠️ 防止任务重叠

0 * * * * flock -n /tmp/script.lock -c '/path/to/script.sh'

使用文件锁防止脚本重复执行

🔍 调试cron任务

# 检查cron服务状态
systemctl status cron
# 查看cron日志
tail -f /var/log/cron

调试定时任务的常用方法

📊 特殊时间表达式

@reboot /path/to/startup.sh # 系统启动时执行
@yearly /path/to/yearly.sh # 每年执行
@monthly /path/to/monthly.sh # 每月执行
@weekly /path/to/weekly.sh # 每周执行
@daily /path/to/daily.sh # 每天执行
@hourly /path/to/hourly.sh # 每小时执行

使用特殊字符串简化时间表达式

⚡ 性能优化建议

# 避免在系统繁忙时执行
5 2 * * * /path/to/heavy-task.sh
# 分散执行时间避免同时运行多个任务
7 3 * * * /path/to/task1.sh
13 3 * * * /path/to/task2.sh

合理安排任务执行时间