cron

chaterm/terminal-skills · updated Apr 8, 2026

MDX-style export adds YAML metadata + attribution linking explainx.ai and this canonical listing URL.

$npx skills add https://github.com/chaterm/terminal-skills --skill cron
0 commentsdiscussion
summary

Schedule and manage cron jobs with configuration, logging, and troubleshooting guidance.

  • Covers crontab syntax, time formats, special characters, and predefined scheduling shortcuts like @daily, @hourly, and @reboot
  • Supports user crontabs, system-wide crontab files, /etc/cron.d directories, and predefined hourly/daily/weekly/monthly execution directories
  • Includes best practices for script templates, environment variables, lock files to prevent duplicate execution, and output redirect
skill.md

定时任务管理

概述

Cron 定时任务配置、日志监控、故障排查等技能。

Crontab 基础

管理命令

# 编辑当前用户的 crontab
crontab -e

# 查看当前用户的 crontab
crontab -l

# 删除当前用户的 crontab
crontab -r

# 管理其他用户的 crontab(需要 root)
crontab -u username -e
crontab -u username -l

时间格式

┌───────────── 分钟 (0-59)
│ ┌───────────── 小时 (0-23)
│ │ ┌───────────── 日 (1-31)
│ │ │ ┌───────────── 月 (1-12)
│ │ │ │ ┌───────────── 星期 (0-7, 0和7都是周日)
│ │ │ │ │
* * * * * command

特殊字符

*       # 任意值
,       # 列表 (1,3,5)
-       # 范围 (1-5)
/       # 步长 (*/5 每5分钟)

# 示例
0 * * * *       # 每小时整点
*/15 * * * *    # 每15分钟
0 9-17 * * *    # 9点到17点每小时
0 0 * * 1-5     # 工作日零点
0 0 1,15 * *    # 每月1号和15号零点

特殊时间字符串

@reboot         # 系统启动时
@yearly         # 每年 (0 0 1 1 *)
@monthly        # 每月 (0 0 1 * *)
@weekly         # 每周 (0 0 * * 0)
@daily          # 每天 (0 0 * * *)
@hourly         # 每小时 (0 * * * *)

配置文件

用户 crontab

# 位置
/var/spool/cron/crontabs/username   # Debian/Ubuntu
/var/spool/cron/username            # CentOS/RHEL

# 格式
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=[email protected]

# 任务
0 2 * * * /usr/local/bin/backup.sh

系统 crontab

# /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 格式:多了用户字段
# 分 时 日 月 周 用户 命令
0 2 * * * root /usr/local/bin/backup.sh

cron.d 目录

# /etc/cron.d/myapp
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin

0 * * * * appuser /opt/myapp/hourly-task.sh
0 2 * * * root /opt/myapp/daily-backup.sh

预定义目录

/etc/cron.hourly/       # 每小时执行
/etc/cron.daily/        # 每天执行
/etc/cron.weekly/       # 每周执行
/etc/cron.monthly/      # 每月执行

# 放入可执行脚本即可
chmod +x /etc/cron.daily/myscript

最佳实践

脚本模板

#!/bin/bash
# /usr/local/bin/cron-task.sh

# 日志文件
LOG_FILE="/var/log/cron-task.log"

# 锁文件(防止重复执行)
LOCK_FILE="/var/run/cron-task.lock"

# 检查锁
if [ -f "$LOCK_FILE" ]; then
    echo "$(date): Task already running" >> "$LOG_FILE"
    exit 1
fi

# 创建锁
trap "rm -f $LOCK_FILE" EXIT
touch "$LOCK_FILE"

# 记录开始
echo "$(date): Task started" >> "$LOG_FILE"

# 执行任务
/path/to/actual/command >> "$LOG_FILE" 2>&1
EXIT_CODE=$?

# 记录结束
echo "$(date): Task finished with exit code $EXIT_CODE" >> "$LOG_FILE"

exit $EXIT_CODE

Crontab 条目

# 推荐写法
# 1. 使用绝对路径
# 2. 重定向输出
# 3. 添加注释

# 每日备份 - 凌晨2点
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

# 每5分钟健康检查
*/5 * * * * /usr/local/bin/healthcheck.sh > /dev/null 2>&1

# 每周日志清理 - 周日凌晨3点
0 3 * * 0 /usr/local/bin/cleanup-logs.sh >> /var/log/cleanup.log 2>&1

环境变量

# 在 crontab 中设置
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
HOME=/home/user
MAILTO=[email protected]

# 或在脚本中加载
#!/bin/bash
source /etc/profile
source ~/.bashrc

日志与监控

查看日志

# 系统日志
grep CRON /var/log/syslog           # Debian/Ubuntu
grep CRON /var/log/cron             # CentOS/RHEL

# 实时监控
tail -f /var/log/syslog | grep CRON

# 查看邮件(如果配置了 MAILTO)
cat /var/mail/username

调试技巧

# 手动测试脚本
/usr/local/bin/myscript.sh

# 模拟 cron 环境
env -i /bin/bash --noprofile --norc -c '/usr/local/bin/myscript.sh'

# 检查 cron 服务状态
systemctl status cron               # Debian/Ubuntu
systemctl status crond              # CentOS/RHEL

常见场景

场景 1:数据库备份

# 每天凌晨2点备份 MySQL
0 2 * * * /usr/bin/mysqldump -u root -p'password' database | gzip > /backup/db_$(date +\%Y\%m\%d).sql.gz

# 注意:% 需要转义为 \%

场景 2:日志轮转

# 每天压缩并清理7天前的日志
0 0 * * * find /var/log/myapp -name "*.log" -mtime +7 -delete
0 1 * * * gzip /var/log/myapp/*.log.1

场景 3:监控告警

# 每5分钟检查服务状态
*/5 * * * * /usr/local/bin/check-service.sh || /usr/local/bin/send-alert.sh

场景 4:使用 flock 防止重复

# 使用 flock 确保单实例运行
*/5 * * * * /usr/bin/flock -n /var/lock/mytask.lock /usr/local/bin/mytask.sh

故障排查

问题 排查方法
任务不执行 检查 cron 服务状态、日志
权限错误 检查脚本权限、用户权限
环境变量问题 在脚本中设置 PATH
命令找不到 使用绝对路径
输出丢失 重定向到日志文件
# 检查 cron 服务
systemctl status cron

# 检查用户是否被禁止
cat /etc/cron.allow
cat /etc/cron.deny

# 检查语法
crontab -l | grep -v '^#' | while read line; do
    echo "Checking: $line"
done
how to use cron

How to use cron on Cursor

AI-first code editor with Composer

1

Prerequisites

Before installing skills in Cursor, ensure your development environment meets these requirements:

  • Cursor installed and configured on your development machine
  • Node.js version 16.0+ with npm package manager (verify with node --version)
  • Active project directory or workspace where you want to add cron
2

Execute installation command

Execute the skills CLI command in your project's root directory to begin installation:

$npx skills add https://github.com/chaterm/terminal-skills --skill cron

The skills CLI fetches cron from GitHub repository chaterm/terminal-skills and configures it for Cursor.

3

Select Cursor when prompted

The CLI will show a list of available agents. Use arrow keys to navigate and space to select Cursor:

◆ Which agents do you want to install to?
│ ── Universal (.agents/skills) ── always included ────
│ • Amp
│ • Antigravity
│ • Cline
│ • Codex
│ ●Cursor(selected)
│ • Cursor
│ • Windsurf
4

Verify installation

Confirm successful installation by checking the skill directory location:

.cursor/skills/cron

Reload or restart Cursor to activate cron. Access the skill through slash commands (e.g., /cron) or your agent's skill management interface.

Security & Verification Notice

We perform automated surface-level scans (Gen AI Scanner, Socket, Snyk) during installation. These checks detect common vulnerabilities but do not guarantee complete security. Always review skill source code and verify the publisher's reputation before production use.

Skills execute code in your development environment. Always verify the publisher's identity, review recent commits, and test in isolated environments before production deployment.

List & Monetize Your Skill

Submit your Claude Code skill and start earning

GET_STARTED →

Use Cases

User Story & Requirements Generation

Create detailed user stories, acceptance criteria, and feature specs

Example

Generate user stories for 'password reset feature' with acceptance criteria, edge cases, and test scenarios

Reduce spec writing time by 50%, ensure comprehensive coverage

Competitive Analysis

Research competitors, compare features, identify gaps

Example

Analyze 5 competitor products, create feature comparison matrix, suggest differentiation opportunities

Complete competitive research in 2 hours instead of 2 days

Roadmap Prioritization

Evaluate features using frameworks (RICE, ICE, Kano) and create prioritized backlogs

Example

Score 20 feature ideas using RICE framework, generate prioritized roadmap with rationale

Make data-driven prioritization decisions faster

Stakeholder Communication

Draft PRDs, status updates, and stakeholder presentations

Example

Create executive summary of Q3 roadmap, monthly progress report, feature launch announcement

Save 3-5 hours/week on communication overhead

Implementation Guide

Prerequisites

  • Claude Desktop or compatible AI client
  • Access to product documentation and roadmap tools (Jira, Notion, etc.)
  • Understanding of product management frameworks (RICE, Jobs-to-be-Done, etc.)
  • Stakeholder contact information and communication channels

Time Estimate

30-60 minutes to see productivity improvements

Installation Steps

  1. 1.Install product management skill
  2. 2.Start with user story generation for known feature
  3. 3.Progress to competitive analysis: research 2-3 competitors
  4. 4.Use for roadmap prioritization: apply RICE/ICE scoring
  5. 5.Draft stakeholder communications and refine based on feedback
  6. 6.Build template library for recurring PM tasks
  7. 7.Share effective prompts with product team

Common Pitfalls

  • Not validating competitive research—verify facts before sharing
  • Accepting user stories without involving engineering team
  • Over-relying on frameworks without qualitative judgment
  • Not customizing outputs to company culture and communication style
  • Skipping stakeholder validation of generated requirements

Best Practices

✓ Do

  • +Validate research and competitive analysis with real data
  • +Collaborate with engineering when generating technical requirements
  • +Customize frameworks and templates to your company context
  • +Use skill for first drafts, refine with stakeholder input
  • +Document successful prompt patterns for PM tasks
  • +Combine AI efficiency with human judgment and intuition

✗ Don't

  • Don't publish competitive analysis without fact-checking
  • Don't finalize user stories without engineering review
  • Don't make prioritization decisions solely on AI scoring
  • Don't skip customer validation of generated requirements
  • Don't ignore company-specific context and culture

💡 Pro Tips

  • Provide context: company goals, constraints, customer feedback
  • Ask for alternatives: 'Show 3 ways to prioritize this roadmap'
  • Request stakeholder-specific formatting: 'Executive summary vs. engineering spec'
  • Use skill for 70% generation + 30% customization to company needs

When to Use This

✓ Use When

Use for user story writing, competitive research, roadmap prioritization, stakeholder communication, and PRD drafting. Best for reducing repetitive documentation and research work.

✗ Avoid When

Avoid for strategic product vision (requires deep customer empathy), pricing decisions (needs market and financial expertise), or when face-to-face customer discovery is more valuable than speed.

Learning Path

  1. 1Basic: user stories, feature specs, status updates
  2. 2Intermediate: competitive analysis, prioritization frameworks, PRDs
  3. 3Advanced: product strategy, go-to-market planning, OKR setting
  4. 4Expert: product vision, market positioning, business model innovation

Discussion

Product Hunt–style comments (not star reviews)
  • No comments yet — start the thread.
general reviews

Ratings

4.751 reviews
  • Kabir Gupta· Dec 28, 2024

    cron is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Kaira Desai· Dec 24, 2024

    Registry listing for cron matched our evaluation — installs cleanly and behaves as described in the markdown.

  • Diya Shah· Dec 20, 2024

    cron has been reliable in day-to-day use. Documentation quality is above average for community skills.

  • Amina Robinson· Dec 20, 2024

    cron reduced setup friction for our internal harness; good balance of opinion and flexibility.

  • Chinedu Martinez· Dec 4, 2024

    cron reduced setup friction for our internal harness; good balance of opinion and flexibility.

  • Zara Jain· Nov 23, 2024

    cron has been reliable in day-to-day use. Documentation quality is above average for community skills.

  • Yuki Khanna· Nov 19, 2024

    Keeps context tight: cron is the kind of skill you can hand to a new teammate without a long onboarding doc.

  • Chinedu Thomas· Nov 11, 2024

    cron reduced setup friction for our internal harness; good balance of opinion and flexibility.

  • Yash Thakker· Nov 3, 2024

    Useful defaults in cron — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Dhruvi Jain· Oct 22, 2024

    Registry listing for cron matched our evaluation — installs cleanly and behaves as described in the markdown.

showing 1-10 of 51

1 / 6