简化定时任务说明,去掉过度设计的双文件方案
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ff1f9ae07f
commit
2f5a9f7035
@ -110,80 +110,41 @@ Claude 会自动执行以下流程:
|
|||||||
|
|
||||||
## 定时自动发送(Cron)
|
## 定时自动发送(Cron)
|
||||||
|
|
||||||
通过 `claude -p` 可以以非交互模式运行技能,配合定时任务实现每周自动发送。
|
通过 `claude -p` 非交互模式配合定时任务实现每周自动发送。
|
||||||
|
|
||||||
### 安全存储凭证
|
新建 `~/weekly_report.sh`,把密码直接写在脚本里,设置好权限确保只有自己可读:
|
||||||
|
|
||||||
**不要**把密码直接写在脚本里,应单独存放在权限受限的凭证文件中:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 创建凭证文件(只存在于本机,不入 git)
|
|
||||||
cat > ~/.ieslab_credentials << 'EOF'
|
|
||||||
export SMTP_USER=your@email.com
|
|
||||||
export SMTP_PASSWORD=your-auth-code
|
|
||||||
export GITEA_TOKEN=your-gitea-token
|
|
||||||
EOF
|
|
||||||
|
|
||||||
# 限制只有当前用户可读
|
|
||||||
chmod 600 ~/.ieslab_credentials
|
|
||||||
```
|
|
||||||
|
|
||||||
### 创建执行脚本
|
|
||||||
|
|
||||||
新建 `~/weekly_report.sh`:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
export SMTP_USER=your@email.com
|
||||||
|
export SMTP_PASSWORD=your-auth-code
|
||||||
|
export GITEA_TOKEN=your-gitea-token # 私有仓库才需要
|
||||||
|
|
||||||
# 加载凭证(敏感信息在此文件中,不在本脚本里)
|
|
||||||
source ~/.ieslab_credentials
|
|
||||||
|
|
||||||
# 切换到目标项目目录
|
|
||||||
cd /path/to/your-project
|
cd /path/to/your-project
|
||||||
|
|
||||||
# 以非交互模式运行技能
|
|
||||||
claude -p "/weekly-report"
|
claude -p "/weekly-report"
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
chmod +x ~/weekly_report.sh
|
chmod 600 ~/weekly_report.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
> `claude -p` 是 Claude Code 的非交互模式,执行完成后自动退出,适合在定时任务中使用。
|
### Windows(任务计划程序)
|
||||||
|
|
||||||
### Windows 定时任务(Task Scheduler)
|
|
||||||
|
|
||||||
Windows 没有 cron,使用"任务计划程序"代替:
|
|
||||||
|
|
||||||
1. 开始菜单搜索「任务计划程序」→「创建基本任务」
|
1. 开始菜单搜索「任务计划程序」→「创建基本任务」
|
||||||
2. 触发器:选择「每周」,设置具体时间(如每周五 18:00)
|
2. 触发器:每周,设置具体时间(如每周五 18:00)
|
||||||
3. 操作:选择「启动程序」
|
3. 操作:启动程序
|
||||||
- 程序:`C:\Program Files\Git\bin\bash.exe`
|
- 程序:`C:\Program Files\Git\bin\bash.exe`
|
||||||
- 参数:`-l -c "/c/Users/你的用户名/weekly_report.sh"`
|
- 参数:`-l -c "/c/Users/你的用户名/weekly_report.sh"`
|
||||||
4. 在「条件」中取消勾选「只在使用交流电源时运行」
|
|
||||||
|
|
||||||
### Linux / Mac(crontab)
|
### Linux / Mac(crontab)
|
||||||
|
|
||||||
如果在 Linux 服务器上运行:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
crontab -e
|
crontab -e
|
||||||
```
|
# 添加(每周五 18:00):
|
||||||
|
|
||||||
添加一行(每周五 18:00 执行):
|
|
||||||
|
|
||||||
```
|
|
||||||
0 18 * * 5 /bin/bash ~/weekly_report.sh >> ~/weekly_report.log 2>&1
|
0 18 * * 5 /bin/bash ~/weekly_report.sh >> ~/weekly_report.log 2>&1
|
||||||
```
|
```
|
||||||
|
|
||||||
`>> ~/weekly_report.log 2>&1` 会把输出和错误都记录到日志文件,方便排查问题。
|
> 如遇「命令未找到」,将 `claude` 替换为绝对路径(用 `which claude` 查询)。
|
||||||
|
|
||||||
### 注意事项
|
|
||||||
|
|
||||||
- 凭证文件 `~/.ieslab_credentials` 必须 `chmod 600`,且**绝不能提交到 git**
|
|
||||||
- 脚本中只有 `source ~/.ieslab_credentials`,密码本身不出现在脚本里
|
|
||||||
- 定时任务的运行环境可能缺少 `claude` 的 PATH,如遇「命令未找到」错误,将 `claude` 替换为绝对路径(可用 `which claude` 查询)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user