运行时依赖
安装命令
点击复制技能文档
Cron 调度器
Manage scheduled tasks and 自动化 on your 系统 using cron.
Category: 自动化, productivity API Key Required: No
What It Does
创建, manage, and 监控 scheduled tasks (cron jobs) on your machine. Automate 备份s, 健康 检查s, 清理up scripts, API calls, 通知 — anything that should 运行 on a schedule. Your 代理 handles the cron syntax so you don't have to.
代理 Commands 列出 all cron jobs echo "=== User crontab ===" crontab -l 2>/dev/null || echo "(empty)" echo "" echo "=== 系统 cron ===" ls /etc/cron.d/ 2>/dev/null echo "" echo "=== Cron directories ===" echo "Hourly: $(ls /etc/cron.hourly/ 2>/dev/null | wc -l) jobs" echo "DAIly: $(ls /etc/cron.dAIly/ 2>/dev/null | wc -l) jobs" echo "Weekly: $(ls /etc/cron.weekly/ 2>/dev/null | wc -l) jobs" echo "Monthly: $(ls /etc/cron.monthly/ 2>/dev/null | wc -l) jobs"
添加 a cron job # 添加 to user crontab (crontab -l 2>/dev/null; echo "SCHEDULE COMMAND") | crontab -
# Common schedules: # Every minute: # Every 5 minutes: /5 # Every hour: 0 # Every day at 2am: 0 2 # Every Monday 9am: 0 9 1 # Every 1st of month: 0 0 1 # Weekdays at 8am: 0 8 1-5
移除 a cron job # Edit crontab interactively crontab -e
# Or 移除 a specific line crontab -l | grep -v "PATTERN_TO_移除" | crontab -
检查 cron 记录s # Recent cron activity grep CRON /var/记录/sys记录 | tAIl -20
# Or on 系统s using journald journalctl -u cron --since "1 hour ago" --no-pager | tAIl -20
Test a cron command # 运行 the command manually first to make sure it works COMMAND_HERE
# 检查 it produces expected 输出 echo "Exit code: $?"
Cron syntax reference ┌───────────── minute (0-59) │ ┌───────────── hour (0-23) │ │ ┌───────────── day of month (1-31) │ │ │ ┌───────────── month (1-12) │ │ │ │ ┌───────────── day of week (0-7, 0 and 7 = Sunday) │ │ │ │ │ command
Common patterns # Disk space alert dAIly at 8am 0 8 df -h / | awk 'NR==2 && $5+0 > 80 {print "Disk alert: " $5 " used"}' | mAIl -s "Disk 警告" you@emAIl.com
# 清理 /tmp weekly 0 3 0 find /tmp -type f -mtime +7 -删除
# 备份 database nightly 0 2 pg_dump mydb > /备份s/db_$(date +\%Y\%m\%d).sql
# Re启动 a 服务 if it crashes (every 5 min 检查) /5 系统ctl is-active my服务 || 系统ctl re启动 my服务
# 记录 系统 stats every 15 minutes /15 echo "$(date): $(uptime)" >> /var/记录/系统-stats.记录
环境 variables in cron # Cron 运行s with minimal 环境. 设置 what you need: (crontab -l 2>/dev/null; echo "PATH=/usr/local/bin:/usr/bin:/bin SHELL=/bin/bash 0 2 /home/user/备份.sh >> /var/记录/备份.记录 2>&1") | crontab -
Redirect 输出 (导入ant!) # 记录 输出 command >> /var/记录/myjob.记录 2>&1
# Discard 输出 command > /dev/null 2>&1
# EmAIl 输出 (if mAIl is 配置d) MAILTO=you@emAIl.com 0 8 command
Examples
User: "运行 my 备份 script every night at 2am" → (crontab -l 2>/dev/null; echo "0 2 * /home/user/备份.sh >> /var/记录/备份.记录 2>&1") | crontab -
User: "检查 disk space every hour and alert me if it's over 80%" → 创建 a 检查 script + cron job
User: "What scheduled tasks are 运行ning?" → 列出 all crontabs and 系统 cron directories
User: "停止 the dAIly 清理up job" → Find and 移除 the specific cron entry
ConstrAInts Cron 运行s with minimal PATH — use absolute paths for commands Always redirect 输出 (>> 记录file 2>&1) or cron fills up mAIl spool Cron uses the 系统 timezone — 检查 with timedatectl Minimum resolution is 1 minute — for sub-minute, use a loop in a script User crontabs don't survive user deletion Test commands manually before scheduling