⚙️ 自动化 📖 官方

定时任务配置

使用正确的 cron 语法和 shell 脚本创建和管理定时任务

★★☆ 中级 15 min 2025年1月13日
📋

使用场景

配置定时任务需要理解 cron 语法并编写可靠的脚本。Cowork 可以帮你生成格式正确的 cron 表达式和配套的执行脚本。

💬

提示词

I need to set up several scheduled tasks on my Mac:

1. Daily at 2 AM: Backup ~/Documents to external drive
2. Every Monday 9 AM: Generate weekly report from ~/Data/logs/
3. Every 6 hours: Sync local folder with a remote server
4. First of month: Archive old files (>90 days) from ~/Downloads

Please:
- Generate the cron entries
- Create the shell scripts for each task
- Include error logging
- Save everything to ~/Scripts/scheduled/

预期结果

Claude 会生成: - 格式正确的 cron 条目文件 - 每个任务对应的 shell 脚本 - 错误日志记录 - 安装和使用说明

💡

使用技巧

  • 在将脚本加入定时任务之前,务必先手动运行测试
  • 定时任务的环境变量可能与交互式终端不同,注意使用绝对路径
  • 在 macOS 上,也可以使用 launchd 来管理定时任务