ActivityWatch 0.14.1 常用 CLI 命令与工具详解

一次讲全 ActivityWatch 0.14.1 命令行工具:aw-server/aw-server-rust、aw-client 六大子命令、aw-qt、三个采集器与 aw-notify,附 curl 调 REST API 速查与 aw-client 0.5.15 query bug 排障实录,全部回显实测。

ActivityWatchActivityWatch时间追踪Linux系统2026/10/23

ActivityWatch 0.14.1 常用 CLI 命令与工具详解

目标:把 ActivityWatch 0.14.1 全家桶里的命令行工具一次讲全——服务器主程序 aw-server、官方客户端 CLI aw-client、托盘管理器 aw-qt、三个采集器与通知工具 aw-notify,外加运维常用的 curl 直调 REST API 速查。所有命令与回显均在 Ubuntu 22.04(22.04.5 LTS)+ v0.14.1 实测采集,参数默认值以实测 --help 为准。

一、环境信息

  • 操作系统:Ubuntu 22.04(22.04.5 LTS,x86_64,无图形界面服务器)
  • 软件版本:ActivityWatch v0.14.1(2026-10-10 发布);aw-client 0.5.15(pip 安装)
  • 安装路径:/opt/activitywatch(安装过程见《Ubuntu 22.04 安装 ActivityWatch 0.14.1 指南》)
  • 服务状态:aw-server 由 systemd 托管,监听 5600,curl -s http://localhost:5600/api/0/info 返回 {"hostname": "aw-server01", "version": "v0.14.1", ...}
  • 实测工具来源:zip 全家桶内置二进制(PyInstaller 打包)+ pip 安装的 aw-client

二、工具概览

工具所在路径角色本篇覆盖
aw-server/opt/activitywatch/aw-server/aw-server服务器主程序(Python 版,默认实现)第四章
aw-server-rust/opt/activitywatch/aw-server-rust/aw-server-rust服务器 Rust 实现(可替换 aw-server)4.4
aw-clientpip 安装 /usr/local/bin/aw-client官方 Python 客户端 CLI(buckets/events/heartbeat/query/report)第五章
aw-qt/opt/activitywatch/aw-qt托盘管理器(桌面形态,拉起/停止各模块)第六章
aw-watcher-afk/opt/activitywatch/aw-watcher-afk/aw-watcher-afk键鼠空闲(AFK)采集器7.1
aw-watcher-window/opt/activitywatch/aw-watcher-window/aw-watcher-window前台窗口采集器7.2
aw-watcher-input/opt/activitywatch/aw-watcher-input/aw-watcher-input键鼠原始输入统计采集器7.3
aw-notify/opt/activitywatch/aw-notify/aw-notify桌面通知/日报工具(Rust 编写)7.4
curl系统自带直调 REST API(最通用,脚本化首选)第八章

关键解读:aw-server/aw-qt/watchers 都是 PyInstaller 单文件二进制(自带 Python 运行时,无需系统 Python 依赖);aw-client 是唯一需要 pip 安装的 CLI(它是纯 Python 客户端库附带的命令行入口);服务器(无桌面)形态下采集器与 aw-qt、aw-notify 无法工作,但命令行参数与帮助输出仍可正常查看(本篇即按此边界组织)。

三、前置条件

  • 已完成安装且 aw-server 处于运行状态(systemd 托管,见安装指南);验证:
systemctl is-active aw-server
curl -s http://localhost:5600/api/0/info
active
{"hostname": "aw-server01", "version": "v0.14.1", "testing": false, "device_id": "a34e4cfc-7992-4c03-a7a2-7422da090665", "profile": "default"}
  • aw-client 需 pip 安装(Ubuntu 22.04 自带 python3.10):
apt-get install -y python3-pip
pip3 install aw-client
which aw-client
/usr/local/bin/aw-client
  • 本文示例基于一个已存在的测试桶 test-bucket(type=test),创建与填充方式见 5.4 与第八章。

四、aw-server:服务器主程序命令行

4.1 版本与完整帮助

/opt/activitywatch/aw-server/aw-server --version
aw-server 0.14.1
/opt/activitywatch/aw-server/aw-server --help
usage: aw-server [-h] [--testing] [--profile PROFILE] [--verbose] [--version]
                 [--log-json] [--host HOST] [--port PORT] [--storage STORAGE]
                 [--cors-origins CORS_ORIGINS] [--custom-static CUSTOM_STATIC]

Starts an ActivityWatch server

optional arguments:
  -h, --help            show this help message and exit
  --testing             Run aw-server in testing mode using different ports
                        and database (alias for --profile testing)
  --profile PROFILE     Named instance profile (data, config, port and
                        settings are isolated). --testing is an alias for
                        --profile testing.
  --verbose             Be chatty.
  --version             Print version and quit
  --log-json            Output the logs in JSON format
  --host HOST           Which host address to bind the server to
  --port PORT           Which port to run the server on
  --storage STORAGE     The method to use for storing data. Some methods (such
                        as MongoDB) require specific Python packages to be
                        available (in the MongoDB case: pymongo)
  --cors-origins CORS_ORIGINS
                        CORS origins to allow (as a comma separated list)
  --custom-static CUSTOM_STATIC
                        The custom static directories. Format:
                        watcher_name=path,watcher_name2=path2,...

(首行 PyInstaller 的 pkg_resources is deprecated UserWarning 为打包固定告警,非错误,下同。)

4.2 运行参数实测

参数优先级与逐项含义见《ActivityWatch 0.14.1 配置参数详解》第三章,此处给两个最常用的实测样例。

指定端口启动(实测):

cd /opt/activitywatch && ./aw-server/aw-server --port 5667
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5667
 * Running on http://10.89.59.2:5667  (werkzeug:187)

testing 模式启动(隔离端口 5666 + 独立数据目录 ~/.local/share/activitywatch-testing/,实测回显):

./aw-server/aw-server --testing
2026-10-22 16:36:41 [INFO ]: Using database file: /root/.local/share/activitywatch-testing/aw-server/peewee-sqlite.v2.db  (aw_datastore.storages.peewee:189)
 * Running on http://localhost:5666  (werkzeug:187)

4.3 systemd 场景下的命令行操作与日志

生产由 systemd 托管后,启停不直接敲 aw-server,而是(逐条执行):

systemctl start aw-server
systemctl stop aw-server
systemctl restart aw-server
systemctl status aw-server --no-pager
journalctl -u aw-server -f

status 实测回显(节选首部):

● aw-server.service - ActivityWatch Server (aw-server)
     Loaded: loaded (/etc/systemd/system/aw-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2026-10-22 16:29:22 UTC; 11s ago
   Main PID: 4347 (aw-server)
      Tasks: 1 (limit: 94371)
     Memory: 36.7M

4.4 aw-server-rust:Rust 版服务器

/opt/activitywatch/aw-server-rust/aw-server-rust --version
aw-server 0.14.1

注意输出前缀仍是 aw-server(不带 rust 字样);区分两种实现看 API 回显:Rust 版 /api/0/info 的 version 字段带 (rust) 后缀,实测 {"hostname":"aw-server02","version":"v0.14.1 (rust)",...}。Rust 版启动日志、数据迁移行为与配置差异见安装指南 6.2/6.3 与配置篇 5.9。

五、aw-client:官方 CLI 客户端

5.1 帮助与子命令清单

aw-client --help
Usage: aw-client [OPTIONS] COMMAND [ARGS]...

  CLI utility for aw-client to aid in interacting with the ActivityWatch
  server

Options:
  --host TEXT     Address of host
  --port INTEGER  Port to use
  -v, --verbose   Verbosity
  --testing       Set to use testing ports by default
  --help          Show this message and exit.

Commands:
  buckets    List all buckets
  canonical  Query 'canonical events' for a single host (filtered,...
  events     Query events from bucket with ID `bucket_id`
  heartbeat  Send a heartbeat to bucket with ID `bucket_id` with JSON `data`
  query      Run a query in file at `path` on the server
  report     Generate an activity report
pip3 show aw-client | head -2
Name: aw-client
Version: 0.5.15

顶层参数:--host(默认 127.0.0.1)、--port(默认 5600)、--testing(默认连 5666)。下文示例先创建一个测试桶并塞入心跳(命令见 5.4 与第八章),再逐个演示。

5.2 aw-client buckets —— 列出全部桶

aw-client buckets
Buckets:
 - test-bucket

每个桶一行;桶是 watcher 与服务器之间的数据载体(模型解释见《ActivityWatch 0.14.1 介绍与选型指南》第三章)。

5.3 aw-client events —— 读取桶内事件

aw-client events test-bucket
events:
 - 2026-10-22 10:05:00 (0:00:00) {'status': 'not-afk'}
 - 2026-10-22 10:00:00 (0:00:30) {'status': 'not-afk'}

输出格式为「时间戳 (时长) {data}」,按时间倒序。events 子命令没有额外过滤参数(--help 实测仅 --help 一项),精细过滤走 query 或第八章的 REST API。

5.4 aw-client heartbeat —— 发送心跳

aw-client heartbeat test-bucket '{"status": "cli-test"}' --pulsetime 10
{'id': None, 'timestamp': datetime.datetime(2026, 10, 22, 16, 18, 24, 658000, tzinfo=datetime.timezone.utc), 'duration': datetime.timedelta(0), 'data': {'status': 'cli-test'}}

参数:桶 ID、JSON 事件数据;pulsetime(秒)定义该心跳的"存活时间"——后续事件在窗口内到达则合并时长(心跳合并机制实测见《ActivityWatch 0.14.1 API 使用详解》)。发完再查 events 可见新事件。

5.5 aw-client query —— 执行查询文件(0.5.15 存在 bug,附绕行)

query 子命令从文件读查询脚本并提交服务器。先写查询文件:

cat > /tmp/testquery.q <<'EOF'
events = query_bucket("test-bucket");
merged = merge_events_by_keys(events, ["status"]);
RETURN = merged;
EOF

执行(0.5.15 实测报错):

aw-client query /tmp/testquery.q
TypeError: query() got an unexpected keyword argument 'json'

排障结论(实测源码核对):0.5.15 的 cli.py 给 query 命令声明了 @click.option("--json", is_flag=True),该参数映射为函数参数 json,而函数签名里写的是 _json: bool——名字对不上,任何调用都必然抛 TypeError,与是否传 --json 无关。这是 0.5.15 的确定缺陷。

绕行方案(两条实测路径):

  • 用 curl 直接提交查询文件(第八章 8.4);
  • 用 Python API:
from datetime import datetime, timedelta, timezone
from aw_client.client import ActivityWatchClient
c = ActivityWatchClient("cli-demo")
start = datetime(2026, 10, 22, 0, 0, tzinfo=timezone.utc)
end = start + timedelta(days=1)
print(c.query('events = query_bucket("test-bucket"); RETURN = events;', [(start, end)]))

5.6 aw-client canonical 与 report —— 依赖窗口数据的两个命令

canonical(过滤+分类后的"规范事件")与 report(活动报告)都按主机名查询窗口/AFK 桶。服务器上没有窗口数据,实测报错(符合预期,正好说明其数据依赖):

aw-client report aw-server01 --start 2026-10-22 --stop 2026-10-23
INFO:aw_client.cli:Querying between 2026-10-22 00:00:00 and 2026-10-23 00:00:00
Traceback (most recent call last):
  ...

(跟踪栈显示 query 阶段 400/无数据。两命令参数一致处:--start/--stop 支持 %Y-%m-%d 等三种格式、--cache 启用本地缓存;差异处:实测 --help,report 多一个 --limit INTEGER,canonical 没有。)日常这两条在桌面机(有 aw-watcher-window_<主机名> 桶)上才有意义,服务器上用 8.4 的自定义 query 代替。

5.7 连接非默认服务器

aw-client --host 10.89.59.2 --port 5600 buckets
aw-client --testing buckets

--testing 等价于连 127.0.0.1:5666(testing 实例),适合桌面机上做不污染生产库的演练。

六、aw-qt:托盘管理器(桌面形态)

6.1 完整帮助

/opt/activitywatch/aw-qt --help
Usage: aw-qt [OPTIONS]

  A trayicon and service manager for ActivityWatch

Options:
  --testing                 Run the trayicon and services in testing mode
  --profile TEXT            Run an isolated instance under this profile name
                            (data, config, port and lockfile are separate).
                            --testing is an alias for --profile testing.
  -v, --verbose             Run with debug logging
  --autostart-modules TEXT  A comma-separated list of modules to autostart, or
                            just `none` to not autostart anything.
  --no-gui                  Start aw-qt without a graphical user interface
                            (terminal output only)
  -i, --interactive         Start aw-qt in interactive cli mode (forces --no-
                            gui)
  --help                    Show this message and exit.

参数速读:--autostart-modules aw-server,aw-watcher-afk 精确指定要拉起的模块(none 表示一个都不拉);--no-gui 只跑管理器不出托盘图标;-i/--interactive 进入终端交互模式(强制 no-gui)。自启动模块清单的持久化配置在 ~/.config/activitywatch/aw-qt/aw-qt.toml(见配置篇 5.10)。

6.2 交互模式实测(无桌面服务器可复现)

cd /opt/activitywatch && ./aw-qt --interactive

实测输出(节选首部):

2026-10-22 16:19:07 [INFO ]: Started aw-qt...  (aw_qt.main:108)
2026-10-22 16:19:07 [WARNING]: Found matching file but was not executable: /opt/activitywatch/aw-server-rust/aw-server.service  (aw_qt.manager:72)
2026-10-22 16:19:07 [INFO ]: Found 7 bundled modules  (aw_qt.manager:97)
2026-10-22 16:19:07 [INFO ]: Found 0 system modules  (aw_qt.manager:145)
2026-10-22 16:19:07 [INFO ]: Starting module aw-server  (aw_qt.manager:217)
2026-10-22 16:19:07 [INFO ]: aw-server: server already running on port 5600, using existing instance instead of starting a new one  (aw_qt.manager:224)
2026-10-22 16:19:07 [INFO ]: Starting module aw-watcher-window  (aw_qt.manager:217)
2026-10-22 16:19:07 [INFO ]: Starting module aw-watcher-afk  (aw_qt.manager:217)
>

三条有价值的事实:aw-qt 共识别 7 个内置模块;拉起 aw-server 前会探测 5600 端口、已有实例时直接复用不重复启动;交互模式以 > 提示符等待,输入 ? 可看可用命令、Aborted!/Ctrl-C 退出。⚠️ 桌面形态下被拉起的窗口/AFK 采集器需要图形会话;无桌面服务器上采集器会因找不到 X 显示而退出(AFK 采集器实测崩溃点在 aw_watcher_afk/unix.py 的 seconds_since_last_input),这正是服务器形态只托管 aw-server 的原因。

七、采集器与通知工具命令行

7.1 aw-watcher-afk —— AFK 采集器

/opt/activitywatch/aw-watcher-afk/aw-watcher-afk --help
usage: aw-watcher-afk [-h] [--host HOST] [--port PORT] [--testing] [--verbose]
                      [--timeout TIMEOUT] [--poll-time POLL_TIME]

A watcher for keyboard and mouse input to detect AFK state.

optional arguments:
  -h, --help            show this help message and exit
  --host HOST
  --port PORT
  --testing             run in testing mode
  --verbose             run with verbose logging
  --timeout TIMEOUT
  --poll-time POLL_TIME

--timeout(判定离开的空闲秒数,默认 180)与 --poll-time(采样间隔秒,默认 5)的默认值来自其配置文件(配置篇 5.11);--host/--port 指向 aw-server。实际运行还需桌面会话(X11),服务器上无法采集。

7.2 aw-watcher-window —— 前台窗口采集器

/opt/activitywatch/aw-watcher-window/aw-watcher-window --help
usage: aw-watcher-window [-h] [--host HOST] [--port PORT] [--testing]
                         [--exclude-title] [--exclude-titles EXCLUDE_TITLES [EXCLUDE_TITLES ...]]
                         [--verbose] [--poll-time POLL_TIME]
                         [--strategy {jxa,applescript,swift}] [--research]
                         [--no-research]

optional arguments:
  -h, --help            show this help message and exit
  --host HOST
  --port PORT
  --testing
  --exclude-title
  --exclude-titles EXCLUDE_TITLES [EXCLUDE_TITLES ...]
                        Exclude window titles by regular expression. Can
                        specify multiple times.
  --verbose
  --poll-time POLL_TIME
  --strategy {jxa,applescript,swift}
                        (macOS only) strategy to use for retrieving the active
                        window
  --research            Enable Research Edition mode: browser titles are
                        classified into study categories, non-browser titles
                        are dropped. Category map must be set in the config
                        file.
  --no-research         Disable Research Edition mode, even when enabled in
                        the config file.

要点:--exclude-titles 接正则、可多次指定;--strategy 三选一但仅 macOS 生效;--research/--no-research 是 Research Edition 的学习分类模式开关(帮助原文见上)。

7.3 aw-watcher-input —— 原始输入统计采集器

/opt/activitywatch/aw-watcher-input/aw-watcher-input --help
Usage: aw-watcher-input [OPTIONS]

Options:
  --host TEXT     Hostname of the aw-server to connect to.
  --port INTEGER  Port of the aw-server to connect to.
  --testing
  --help          Show this message and exit.

注意它是 click 风格(与 afk/window 的 argparse 风格不同)。该采集器统计键盘鼠标的原始输入量(非内容),同样依赖桌面会话。

7.4 aw-notify —— 通知与日报工具

/opt/activitywatch/aw-notify/aw-notify --version
aw-notify 0.2.0
/opt/activitywatch/aw-notify/aw-notify --help
ActivityWatch notification service

Provides desktop notifications for time tracking data from ActivityWatch.
Use --output-only to print notifications to stdout instead of showing desktop notifications (useful for scripting or integration with other tools(aw-tauri)).

Usage: aw-notify [OPTIONS] [COMMAND]

Commands:
  start             Start the notification service (use --output-only to print notifications instead of showing them)
  checkin           Send a summary notification (use --output-only to print to stdout instead of showing notification)
  checkin-detailed  Send a detailed summary with all category levels (parent and leaf categories)
  help              Print this message or the help of the given subcommand(s)

Options:
  -v, --verbose
          Verbose logging

  -c, --config <CONFIG>
          Path to custom configuration file

      --testing
          Testing mode (port 5666)

      --port <PORT>
          Port to connect to ActivityWatch server

  -o, --output-only
          Only print JSON to stdout, no desktop notifications

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

两个实测要点:

  • 参数顺序坑:-o 是顶层选项,必须放在子命令前。实测 aw-notify checkin -o 报错、aw-notify -o checkin 正常:
error: unexpected argument '-o' found

Usage: aw-notify checkin [OPTIONS]
  • 首次运行自动生成配置 ~/.config/activitywatch/aw-notify/config.toml(enabled = false、hourly_checkins/new_day_greetings/productivity_score = true、四组 alerts 阈值表);且日志提示"Loaded configuration from ActivityWatch settings API (overrides local TOML)"。通知未启用时(enabled = false),aw-notify -o checkin 正常退出但 stdout 无 JSON 输出——先在其设置中置 enabled = true。

八、curl 调 REST API 速查(运维必备)

本节是「无客户端依赖」的脚本化路径,全部命令在 5600 实测通过;完整的端点、参数与错误码体系见《ActivityWatch 0.14.1 API 使用详解》。

8.1 查看服务器信息与桶列表

curl -s http://localhost:5600/api/0/info
{"hostname": "aw-server01", "version": "v0.14.1", "testing": false, "device_id": "a34e4cfc-7992-4c03-a7a2-7422da090665", "profile": "default"}
curl -s http://localhost:5600/api/0/buckets/
{"test-bucket": {"id": "test-bucket", "created": "2026-10-22T16:14:56.717943+00:00", "name": null, "type": "test", "client": "aw-client-test", "hostname": "aw-server01", "data": {}, "last_updated": "2026-10-22T16:18:24.658000+00:00"}}

8.2 创建桶与发送心跳

curl -s -X POST -H "Content-Type: application/json" -d '{"client": "aw-client-test", "type": "test", "hostname": "aw-server01"}' http://localhost:5600/api/0/buckets/test-bucket
{}
curl -s -X POST -H "Content-Type: application/json" -d '{"timestamp": "2026-10-22T10:00:00+00:00", "duration": 0, "data": {"status": "not-afk"}}' "http://localhost:5600/api/0/buckets/test-bucket/heartbeat?pulsetime=120"
{"id": 1, "timestamp": "2026-10-22T10:00:00+00:00", "duration": 0.0, "data": {"status": "not-afk"}}

8.3 读事件

curl -s "http://localhost:5600/api/0/buckets/test-bucket/events?limit=5"
[{"id": 1, "timestamp": "2026-10-22T10:00:00+00:00", "duration": 0.0, "data": {"status": "not-afk"}}]

8.4 执行查询(canonical query)

curl -s -X POST -H "Content-Type: application/json" -d '{"timeperiods": ["2026-10-22T00:00:00+00:00/2026-10-23T00:00:00+00:00"], "query": ["events = query_bucket(\"test-bucket\"); merged = merge_events_by_keys(events, [\"status\"]); dur = sum_durations(events); RETURN = [merged, dur];"]}' http://localhost:5600/api/0/query/
[[[{"data":{"status":"not-afk"},"duration":30.0,"id":null,"timestamp":"2026-10-22T10:05:00+00:00"}],30.0]]

要点(均为实测踩坑结论):timeperiods 必填,缺了报 {"errors": {"timeperiods": "'timeperiods' is a required property"}, "message": "Input payload validation failed"};query_bucket 的 limit 用位置参数(写 limit=1 命名参数会报 QueryParseException: Expected , between arguments to query_bucket, got: =1)。

8.5 导出与认证

curl -s http://localhost:5600/api/0/buckets/test-bucket/export
{"buckets": {"test-bucket": {"id": "test-bucket", "created": "2026-10-22T16:14:56.717943+00:00", ..., "events": [...]}}}

(节选,桶定义+全部事件。)服务器启用 [server.auth] api_key 后,以上除 /api/0/info 外的请求都要加头,实测缺头返回 401:

curl -s -H "Authorization: Bearer q1w2e3r4" http://localhost:5600/api/0/buckets/
{"test-bucket": {...}}

九、注意事项

  • aw-client CLI 与 REST API 的分工:日常速查用 aw-client;脚本化、过滤聚合、错误码处理用 curl/REST API(稳定且不受 CLI bug 影响);
  • aw-client 0.5.15 的 query 子命令不可用(第五章 5.5),脚本里不要依赖它;
  • 参数风格不统一是常态:aw-server/watchers 是 argparse 风格(--help 为 usage 形态),aw-qt/aw-watcher-input/aw-notify 是 click/clap 风格——以各自 --help 实际输出为准,不要凭感觉混写;
  • 顶层选项在子命令前:aw-notify 的 -o(7.4 实测)、aw-client 的 --host/--port 都要写在子命令之前;
  • 服务器形态的能力边界:watchers/aw-qt/aw-notify 的运行时能力依赖桌面会话;服务器上只跑 aw-server + aw-client/curl;
  • testing 模式全程隔离:端口 5666 + 独立数据库目录,演练用 --testing(aw-server)或 --testing(aw-client),不碰生产数据。

十、常见问题(FAQ)

Q1:aw-client buckets 报连接拒绝?

aw-server 没在运行或不在默认端口。先 systemctl is-active aw-server,再 curl -s http://localhost:5600/api/0/info 探活;非默认端口加 --port,非本机加 --host(顺序:顶层选项在子命令前)。

Q2:aw-server 已有实例在跑,手动再启一个报端口占用?

aw-server 不做单例锁,重复启动会直接撞 5600 端口,报错原文与处置(kill 旧进程或停 systemd 服务)见安装指南 FAQ Q1。aw-qt 则相反,会探测端口并复用现有实例(6.2 实测)。

Q3:aw-client query 的 TypeError 什么时候修?

跟踪 aw-client 仓库的修复版本即可(0.5.15 确认存在,见 5.5);期间用 8.4 的 curl 方案,能力完全等价。

Q4:报告/canonical 命令在服务器上报 400?

这两个命令查询 aw-watcher-window_<主机名>/aw-watcher-afk_<主机名> 桶,服务器上没有这些桶(无桌面会话,无人采集)。要"今天的活动报告",在桌面机跑,或用 8.4 对你实际拥有的桶做聚合。

Q5:watcher 的 --host/--port 与配置文件哪个生效?

命令行参数优先于配置文件(与 aw-server 同规则);aw-qt 托管场景改 ~/.config/activitywatch/aw-qt/aw-qt.toml 的模块清单与参数,命令行一次性调试用 --host/--port。

十一、参考链接