cron-manager
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCron Manager Skill
Cron 任务管理技能
Schedule and manage cron jobs.
计划和管理cron任务。
When to Use
适用场景
- Create new scheduled tasks
- List existing cron jobs
- Remove scheduled tasks
- Test cron expressions
- Convert human-readable schedules to cron
- 创建新的定时任务
- 列出已有的cron任务
- 删除定时任务
- 测试cron表达式
- 将人类可读的计划转换为cron表达式
Cron Basics
Cron 基础知识
Cron Expression Format
Cron 表达式格式
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday = 0)
│ │ │ │ │
* * * * *┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday = 0)
│ │ │ │ │
* * * * *Common Patterns
常用模式
| Schedule | Cron | Description |
|---|---|---|
| Every minute | | Run every minute |
| Every hour | | Run at minute 0 of every hour |
| Daily at midnight | | Run at midnight |
| Daily at 9am | | Run at 9am |
| Weekly on Sunday | | Run at midnight Sunday |
| Monthly | | Run at midnight on 1st |
| Every 5 minutes | | Run every 5 minutes |
| Every 30 minutes | | Run every 30 minutes |
| 计划 | Cron表达式 | 描述 |
|---|---|---|
| 每分钟执行一次 | | 每分钟运行一次 |
| 每小时执行一次 | | 在每小时的第0分钟运行 |
| 每日午夜执行 | | 在午夜运行 |
| 每日上午9点执行 | | 在上午9点运行 |
| 每周日执行 | | 在周日午夜运行 |
| 每月执行 | | 在每月1日午夜运行 |
| 每5分钟执行一次 | | 每5分钟运行一次 |
| 每30分钟执行一次 | | 每30分钟运行一次 |
Manage Cron Jobs
管理Cron任务
List User Crontab
列出用户的Crontab
bash
undefinedbash
undefinedList current crontab
列出当前的crontab
crontab -l
crontab -l
List for specific user
列出指定用户的crontab
crontab -l -u username
undefinedcrontab -l -u username
undefinedAdd Cron Job
添加Cron任务
bash
undefinedbash
undefinedEdit crontab
编辑crontab
crontab -e
crontab -e
Add from command line
从命令行添加任务
(crontab -l 2>/dev/null; echo "0 9 * * * /path/to/script.sh") | crontab -
undefined(crontab -l 2>/dev/null; echo "0 9 * * * /path/to/script.sh") | crontab -
undefinedRemove Cron Job
删除Cron任务
bash
undefinedbash
undefinedRemove all crontabs
删除所有crontab任务
crontab -r
crontab -r
Remove for specific user
删除指定用户的crontab任务
crontab -r -u username
undefinedcrontab -r -u username
undefinedSpecific Operations
特定操作
bash
undefinedbash
undefinedRun job immediately (test)
立即运行任务(测试)
/path/to/script.sh
/path/to/script.sh
Check cron service status
检查cron服务状态
systemctl status cron # or crond
systemctl status cron # 或 crond
View cron logs
查看cron日志
journalctl -u cron # systemd
tail -f /var/log/cron # other systems
undefinedjournalctl -u cron # systemd系统
tail -f /var/log/cron # 其他系统
undefinedCron Utilities
Cron 实用工具
Validate Cron Expression
验证Cron表达式
bash
undefinedbash
undefinedUsing Python
使用Python验证
python3 -c "
from croniter import croniter
import sys
if len(sys.argv) > 1:
expr = sys.argv[1]
if croniter.is_valid(expr):
print('Valid cron expression')
# Show next 5 runs
cron = croniter(expr)
for i in range(5):
print(cron.get_next())
else:
print('Invalid cron expression')
"
python3 -c "
from croniter import croniter
import sys
if len(sys.argv) > 1:
expr = sys.argv[1]
if croniter.is_valid(expr):
print('Valid cron expression')
# 显示接下来5次运行时间
cron = croniter(expr)
for i in range(5):
print(cron.get_next())
else:
print('Invalid cron expression')
"
Using online tools or crontab.guru
使用在线工具或crontab.guru
curl -s "https://crontab.guru/0+9+*+*+*"
undefinedcurl -s "https://crontab.guru/0+9+*+*+*"
undefinedHuman-Readable Conversion
人类可读格式转换
bash
undefinedbash
undefinedUsing python-crontab
使用python-crontab库
python3 -c "
from crontab import CronSitemap
c = CronSitemap('0 9 * * *')
print(c.human_readable)
"
undefinedpython3 -c "
from crontab import CronSitemap
c = CronSitemap('0 9 * * *')
print(c.human_readable)
"
undefinedScript Examples
脚本示例
Backup Script Schedule
备份脚本计划
bash
undefinedbash
undefinedAdd daily backup at 2am
添加每日凌晨2点的备份任务
(crontab -l 2>/dev/null; echo "0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1") | crontab -
undefined(crontab -l 2>/dev/null; echo "0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1") | crontab -
undefinedLog Cleanup
日志清理
bash
undefinedbash
undefinedClean logs older than 7 days, daily at 3am
清理7天以上的日志,每日凌晨3点执行
(crontab -l 2>/dev/null; echo "0 3 * * * find /var/log -name '*.log' -mtime +7 -delete") | crontab -
undefined(crontab -l 2>/dev/null; echo "0 3 * * * find /var/log -name '*.log' -mtime +7 -delete") | crontab -
undefinedHealth Check
健康检查
bash
undefinedbash
undefinedCheck every 5 minutes
每5分钟检查一次
(crontab -l 2>/dev/null; echo "*/5 * * * * curl -s https://example.com/health") | crontab -
undefined(crontab -l 2>/dev/null; echo "*/5 * * * * curl -s https://example.com/health") | crontab -
undefinedSpecial Characters
特殊字符
| Character | Meaning |
|---|---|
| Any value |
| List (1,3,5) |
| Range (1-5) |
| Step (*/15 = every 15) |
| 字符 | 含义 |
|---|---|
| 任意值 |
| 列表(如1,3,5) |
| 范围(如1-5) |
| 步长(如*/15 = 每15个单位) |
Notes
注意事项
- Cron jobs run with user's default shell
- Use absolute paths for scripts
- Redirect output to log files
- Consider timezone (cron uses system TZ)
- Thepopebot uses for job scheduling
config/CRONS.json
- cron任务使用用户的默认shell运行
- 脚本请使用绝对路径
- 将输出重定向到日志文件
- 考虑时区问题(cron使用系统时区)
- Thepopebot 使用 进行任务调度
config/CRONS.json