OpenClaw Linux 教程OpenClaw Linux 教程
首页
基础教程
最佳实践
OpenClaw 官网
编程指南
首页
基础教程
最佳实践
OpenClaw 官网
编程指南
  • 基础教程

    • 📚 基础教程
    • 第1章 - 认识 OpenClaw
    • 第2章 - 环境准备
    • 第3章 - 安装 OpenClaw
    • 第4章 - 引导向导配置
    • 第5章 - Gateway 网关
    • 第6章 - 连接聊天渠道
    • 第7章 - 工具与技能
    • 第8章 - 日常使用技巧
    • 第9章 - 自定义模型与阿里云百炼

第8章 - 日常使用技巧

嗨,朋友!恭喜你完成了所有基础章节的学习!这一章我来分享一些日常使用 OpenClaw 的实用技巧,让你用得更顺手。

🚀 常用 CLI 命令速查

这些是你每天都会用到的命令:

# 查看 Gateway 状态
openclaw gateway status

# 启动/停止/重启 Gateway
systemctl --user start openclaw-gateway
systemctl --user stop openclaw-gateway
systemctl --user restart openclaw-gateway

# 发送消息给 AI
openclaw agent --message "你的问题"

# 使用高级思考模式(适合复杂问题)
openclaw agent --message "复杂问题" --thinking high

# 打开 Dashboard
openclaw dashboard

# 健康检查
openclaw doctor

# 查看日志
journalctl --user -u openclaw-gateway -f

# 更新 OpenClaw
openclaw update --channel stable

💡 高效对话技巧

1. 明确你的需求

❌ 差的方式:帮我看看服务器
✅ 好的方式:帮我检查服务器的 CPU 使用率、内存占用和磁盘空间,如果有异常就告诉我

2. 分步骤描述复杂任务

✅ 好的方式:
请帮我完成以下步骤:
1. 备份 /etc/nginx/nginx.conf 到 /tmp/
2. 修改 nginx 配置,添加一个新的 server block,域名是 example.com
3. 测试 nginx 配置是否正确
4. 如果正确就重新加载 nginx

3. 善用思考模式

# 简单问题用默认模式
openclaw agent --message "现在几点了?"

# 复杂问题用高级思考
openclaw agent --message "分析一下过去 7 天的 nginx 访问日志,找出访问最多的 10 个 IP" --thinking high

🔄 开机自启配置

确保 OpenClaw 在系统重启后自动运行:

# 检查 systemd 服务是否已启用
systemctl --user is-enabled openclaw-gateway

# 如果没有启用,手动启用
systemctl --user enable openclaw-gateway

# 关键:让用户服务在注销后继续运行
sudo loginctl enable-linger $(whoami)

注意

enable-linger 很重要!默认情况下,systemd 用户服务会在你注销 SSH 后停止。运行 enable-linger 后,服务会一直运行,即使你断开 SSH 连接。

📊 日常监控脚本

创建一个监控脚本,定期检查 OpenClaw 的运行状态:

#!/bin/bash
# 保存为 ~/check-openclaw.sh

echo "====== OpenClaw 状态检查 ======"
echo "时间: $(date)"
echo ""

# 检查 Gateway 进程
if systemctl --user is-active openclaw-gateway > /dev/null 2>&1; then
    echo "✅ Gateway: 运行中"
else
    echo "❌ Gateway: 已停止"
    echo "   尝试重启..."
    systemctl --user restart openclaw-gateway
fi

# 检查端口
if ss -tlnp | grep -q 18789; then
    echo "✅ 端口 18789: 正在监听"
else
    echo "❌ 端口 18789: 未监听"
fi

# 检查内存使用
echo ""
echo "====== 系统资源 ======"
free -h | head -2
echo ""
df -h / | tail -1

echo ""
echo "====== 最近日志 ======"
journalctl --user -u openclaw-gateway --no-pager -n 5
# 给脚本添加执行权限
chmod +x ~/check-openclaw.sh

# 运行检查
~/check-openclaw.sh

🔔 设置定时任务

让 OpenClaw 每天给你发日报:

你:每天晚上 10 点给我发一份服务器状态报告,包括:
- 系统运行时间
- CPU 使用率
- 内存使用情况
- 磁盘空间
- 今天的 Gateway 日志摘要

或者用系统 crontab 定时检查 OpenClaw 状态:

# 编辑 crontab
crontab -e

# 每 5 分钟检查一次 Gateway 状态,如果挂了就重启
*/5 * * * * systemctl --user is-active openclaw-gateway || systemctl --user restart openclaw-gateway

🔄 版本管理与更新

查看当前版本

openclaw --version

更新到最新稳定版

openclaw update --channel stable

开发频道

频道说明适合谁
stable稳定发布版大多数用户
beta预发布版想尝试新功能
dev开发版开发者/贡献者
# 切换频道
openclaw update --channel beta

# 更新后检查
openclaw doctor

📝 日志管理

查看日志

# 实时查看日志
journalctl --user -u openclaw-gateway -f

# 查看最近 100 行日志
journalctl --user -u openclaw-gateway -n 100

# 查看今天的日志
journalctl --user -u openclaw-gateway --since today

# 查看特定时间段的日志
journalctl --user -u openclaw-gateway --since "2024-01-01" --until "2024-01-02"

日志级别

在启动时可以设置日志详细程度:

# 详细日志(调试用)
openclaw gateway --verbose

# 正常日志(生产环境)
openclaw gateway

🎯 实战场景示例

场景1:服务器运维助手

你:检查一下 nginx 是否正常运行,如果有错误日志就告诉我最近的 10 条

场景2:代码助手

你:帮我写一个 Python 脚本,监控 /var/log/auth.log 中的失败登录尝试,超过 5 次就发通知

场景3:文件管理

你:帮我整理 ~/Downloads 目录,把图片、文档、压缩包分别移到对应的子目录中

场景4:学习助手

你:教我怎么在 Linux 上配置 SSH 密钥认证,一步步来

💡 长安的使用建议

作为过来人,我想给你几点日常使用的建议:

  1. 养成习惯 - 遇到重复性工作就让 OpenClaw 来做
  2. 写好技能 - 把常用的工作流写成 SKILL.md,一句话触发
  3. 定期更新 - OpenClaw 更新很频繁,新功能和修复会不断加入
  4. 关注安全 - 定期运行 openclaw doctor 检查安全配置
  5. 参与社区 - Discord 上有很多热心用户和开发者

💪 练习题

  1. 如何让 OpenClaw Gateway 在 SSH 断开后继续运行?
  2. 写一个 Shell 脚本来自动检查 Gateway 状态并在崩溃时重启。
  3. 描述 3 个你日常工作中可以用 OpenClaw 自动化的场景。

答案提示

  1. 运行 sudo loginctl enable-linger $(whoami) 确保 systemd 用户服务在注销后继续运行
  2. 参考本章"日常监控脚本"部分
  3. 因人而异,可以是日志分析、代码审查、系统监控、文件整理、定时报告等

下一步: 第9章 - 自定义模型与阿里云百炼 →

最近更新: 2026/2/28 09:29
Contributors: 王长安
Prev
第7章 - 工具与技能
Next
第9章 - 自定义模型与阿里云百炼