cron-manager

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cron 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

常用模式

ScheduleCronDescription
Every minute
* * * * *
Run every minute
Every hour
0 * * * *
Run at minute 0 of every hour
Daily at midnight
0 0 * * *
Run at midnight
Daily at 9am
0 9 * * *
Run at 9am
Weekly on Sunday
0 0 * * 0
Run at midnight Sunday
Monthly
0 0 1 * *
Run at midnight on 1st
Every 5 minutes
*/5 * * * *
Run every 5 minutes
Every 30 minutes
*/30 * * * *
Run every 30 minutes
计划Cron表达式描述
每分钟执行一次
* * * * *
每分钟运行一次
每小时执行一次
0 * * * *
在每小时的第0分钟运行
每日午夜执行
0 0 * * *
在午夜运行
每日上午9点执行
0 9 * * *
在上午9点运行
每周日执行
0 0 * * 0
在周日午夜运行
每月执行
0 0 1 * *
在每月1日午夜运行
每5分钟执行一次
*/5 * * * *
每5分钟运行一次
每30分钟执行一次
*/30 * * * *
每30分钟运行一次

Manage Cron Jobs

管理Cron任务

List User Crontab

列出用户的Crontab

bash
undefined
bash
undefined

List current crontab

列出当前的crontab

crontab -l
crontab -l

List for specific user

列出指定用户的crontab

crontab -l -u username
undefined
crontab -l -u username
undefined

Add Cron Job

添加Cron任务

bash
undefined
bash
undefined

Edit 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 -
undefined

Remove Cron Job

删除Cron任务

bash
undefined
bash
undefined

Remove all crontabs

删除所有crontab任务

crontab -r
crontab -r

Remove for specific user

删除指定用户的crontab任务

crontab -r -u username
undefined
crontab -r -u username
undefined

Specific Operations

特定操作

bash
undefined
bash
undefined

Run 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
undefined
journalctl -u cron # systemd系统 tail -f /var/log/cron # 其他系统
undefined

Cron Utilities

Cron 实用工具

Validate Cron Expression

验证Cron表达式

bash
undefined
bash
undefined

Using 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

undefined
undefined

Human-Readable Conversion

人类可读格式转换

bash
undefined
bash
undefined

Using python-crontab

使用python-crontab库

python3 -c " from crontab import CronSitemap c = CronSitemap('0 9 * * *') print(c.human_readable) "
undefined
python3 -c " from crontab import CronSitemap c = CronSitemap('0 9 * * *') print(c.human_readable) "
undefined

Script Examples

脚本示例

Backup Script Schedule

备份脚本计划

bash
undefined
bash
undefined

Add 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 -
undefined

Log Cleanup

日志清理

bash
undefined
bash
undefined

Clean 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 -
undefined

Health Check

健康检查

bash
undefined
bash
undefined

Check 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 -
undefined

Special Characters

特殊字符

CharacterMeaning
*
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
    config/CRONS.json
    for job scheduling
  • cron任务使用用户的默认shell运行
  • 脚本请使用绝对路径
  • 将输出重定向到日志文件
  • 考虑时区问题(cron使用系统时区)
  • Thepopebot 使用
    config/CRONS.json
    进行任务调度