Tryton 8.2 常用 CLI 命令详解
trytond / trytond-admin / trytond-console / trytond-cron 四个命令行工具完整 --help 实测回显与逐参数说明,含初始化、模块激活、console 脚本化写入、cron --once 等真实示例。
Tryton 8.2 常用 CLI 命令详解
目标:详解 trytond 8.2.1 自带的四个命令行工具——trytond(服务进程)、trytond-admin(数据库管理)、trytond-console(交互控制台)、trytond-cron(计划任务),附完整
--help实测回显、逐参数说明与真实使用示例。实测环境 Ubuntu 22.04 + trytond 8.2.1,数据库trytondb,配置文件/etc/tryton/trytond.conf。
一、工具总览
trytond 安装后在 /usr/local/bin/ 下提供四个命令,覆盖「运行服务、管理数据、交互排障、后台任务」四类场景:
| 命令 | 作用 | 典型场景 |
|---|---|---|
trytond | 启动 WSGI 服务进程 | 前台调试或交由 systemd 托管 |
trytond-admin | 数据库初始化、模块激活、用户密码管理 | 部署、升级、日常管理 |
trytond-console | 带 ORM 上下文的 Python 控制台 | 数据查询/修复、脚本化运维 |
trytond-cron | 执行计划任务(调度器) | 定时任务守护或手动触发 |
四个命令版本一致,实测:
trytond --version
trytond-admin --version
trytond-console --version
trytond-cron --version
实测输出:
trytond 8.2.1
trytond-admin 8.2.1
trytond-console 8.2.1
trytond-cron 8.2.1
二、trytond:服务进程
2.1 完整帮助
trytond --help
实测输出:
usage: trytond [-h] [--version] [-c FILE [FILE ...]] [-v] [--dev]
[-d DATABASE [DATABASE ...]] [--logconf FILE] [--pidfile FILE]
[--coroutine] [-n PROCESSES] [--max MAXTASKSPERCHILD]
options:
-h, --help show this help message and exit
--version show program's version number and exit
-c FILE [FILE ...], --config FILE [FILE ...]
specify configuration files
-v, --verbose increase verbosity
--dev enable development mode
-d DATABASE [DATABASE ...], --database DATABASE [DATABASE ...]
specify the database names
--logconf FILE set logging configuration file (ConfigParser format)
--pidfile FILE set file to store the process id
--coroutine use coroutine for concurrency
-n PROCESSES set number of processes to use
--max MAXTASKSPERCHILD
set number of tasks a worker process before being
replaced
The default Decimal precision can be specified in the TRYTOND_DECIMAL_PREC
environment variable.
2.2 关键参数说明
| 参数 | 说明 |
|---|---|
-c FILE | 指定配置文件,可多个(后者覆盖前者同名键);也可由 TRYTOND_CONFIG 环境变量提供 |
-d DATABASE | 预初始化指定数据库的模型连接池,可多个。不预声明时首请求会因惰性初始化失败(登录 500,见安装篇 FAQ Q6),生产启动必带 |
-v / --dev | 提高日志详细度 / 开发模式(自动重载等) |
--logconf FILE | 独立的日志配置文件(ConfigParser 格式) |
--pidfile FILE | PID 文件路径,systemd 托管时配合 PIDFile= 使用 |
-n PROCESSES | 工作进程数(默认单进程) |
--coroutine | 用协程替代多进程并发 |
--max N | worker 处理 N 个任务后自动替换(防内存泄漏累积) |
2.3 实测启动
前台启动(观察日志用):
trytond -c /etc/tryton/trytond.conf -d trytondb
启动日志节选(实测首尾):
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8000
...
... INFO werkzeug Press CTRL+C to quit
监听成功后 Ctrl+C 退出;生产托管见安装篇的 systemd 单元(ExecStart 即本命令)。
三、trytond-admin:数据库管理
部署与升级的核心工具,直接操作 PostgreSQL,运行时不需要 trytond 服务在线。
3.1 完整帮助
trytond-admin --help
实测输出:
usage: trytond-admin [-h] [--version] [-c FILE [FILE ...]] [-v] [--dev]
[-d DATABASE [DATABASE ...]] [--logconf FILE]
[-u MODULE [MODULE ...]] [--indexes | --no-indexes]
[--all] [--activate-dependencies] [--login LOGIN]
[--administrator | --no-administrator]
[--active | --no-active] [--email EMAIL] [-p]
[--reset-password] [--test-email TEST_EMAIL] [-m]
[-l CODE [CODE ...]] [--hostname HOSTNAME]
[--validate [MODEL ...]]
[--validate-percentage PERCENTAGE]
[--export-translations]
options:
-h, --help show this help message and exit
--version show program's version number and exit
-c FILE [FILE ...], --config FILE [FILE ...]
specify configuration files
-v, --verbose increase verbosity
--dev enable development mode
-d DATABASE [DATABASE ...], --database DATABASE [DATABASE ...]
specify the database names
--logconf FILE set logging configuration file (ConfigParser format)
-u MODULE [MODULE ...], --update MODULE [MODULE ...]
activate or update modules
--indexes, --no-indexes
update indexes
--all update all activated modules
--activate-dependencies
activate missing dependencies of updated modules
--login LOGIN login of the user (default: admin)
--administrator, --no-administrator
set the user as administrator
--active, --no-active
activate the user (default: True)
--email EMAIL set the user email
-p, --password set the user password
--reset-password reset the user password
--test-email TEST_EMAIL
send a test email to the specified address
-m, --update-modules-list
update the list of tryton modules
-l CODE [CODE ...], --language CODE [CODE ...]
load language translations
--hostname HOSTNAME limit database listing to the hostname
--validate [MODEL ...]
validate records of models
--validate-percentage PERCENTAGE
percentage of records to validate (default: 100)
--export-translations
export module translations to locale folder
The first time a database is initialized or when the password is set, the user
password is read from file defined by TRYTONPASSFILE environment variable or
interactively asked from the user. The config file can be specified in the
TRYTOND_CONFIG environment variable. The database URI can be specified in the
TRYTOND_DATABASE_URI environment variable.
3.2 初始化新数据库
首次建库(建全部框架表 + 设置 admin 账号):
printf 'q1w2e3r4\nq1w2e3r4\n' | trytond-admin -c /etc/tryton/trytond.conf -d trytondb --all --email admin@example.com -p
-d目标库;--all初始化所有已激活模块(新库即建框架表);--email预设 admin 邮箱避免交互;-p从 stdin 读两行密码;- 免交互替代方案:把密码写入文件并设置
TRYTONPASSFILE=/path/to/file。
实测输出:
6094 ... WARNING py.warnings ... UserWarning: The path specified in the URI will be overridden
email for "admin" of "trytondb" (empty for none):
Warning: Password input may be echoed.
password for "admin" of "trytondb":
Warning: Password input may be echoed.
password confirmation for "admin":
⚠️ 注意:trytond-admin 只操作已存在的数据库,建库动作本身由 PostgreSQL 完成(
createdb),依赖链见安装篇第四节。
3.3 激活/更新模块
trytond-admin -c /etc/tryton/trytond.conf -d trytondb -u sale --activate-dependencies
-u 后接一个或多个模块名,--activate-dependencies 自动补齐依赖。升级 trytond 后对所有已激活模块做 schema 迁移用 --all:
trytond-admin -c /etc/tryton/trytond.conf -d trytondb --all
3.4 刷新模块清单
新装了模块包(pip install trytond-xxx)后,数据库里的模块清单不会自动更新,需手动刷新:
trytond-admin -c /etc/tryton/trytond.conf -d trytondb -m
实测无输出、退出码 0 即成功,新模块随后才能被 -u 激活。
3.5 数据校验
按模型跑一遍 ORM 校验规则(抽检 50% 记录):
trytond-admin -c /etc/tryton/trytond.conf -d trytondb --validate party.party --validate-percentage 50
实测无输出、退出码 0 即全部通过;有记录违反校验规则时会逐条打印。
3.6 用户与密码管理
# 重置 admin 密码(交互输入新密码)
trytond-admin -c /etc/tryton/trytond.conf -d trytondb --reset-password
# 创建/修改用户并设为管理员
trytond-admin -c /etc/tryton/trytond.conf -d trytondb --login demo --administrator --email demo@quzuo.net -p
# 验证 SMTP 配置
trytond-admin -c /etc/tryton/trytond.conf -d trytondb --test-email ops@quzuo.net
3.7 多语言与翻译
# 加载中文翻译
trytond-admin -c /etc/tryton/trytond.conf -d trytondb -l zh_CN
# 导出模块翻译文件到 locale 目录(做汉化包时)
trytond-admin -c /etc/tryton/trytond.conf -d trytondb --export-translations
四、trytond-console:交互控制台
在 ORM 上下文中直接执行 Python,是数据排障与批量修复的利器。
4.1 完整帮助
trytond-console --help
实测输出:
usage: trytond-console [-h] [--version] [-c FILE [FILE ...]] -d DATABASE
[-f FILE] [--histsize HISTSIZE] [--readonly]
[--lock-table TABLE [TABLE ...]]
[--lock-records TABLE=id,id [TABLE=id,id ...]]
options:
-h, --help show this help message and exit
--version show program's version number and exit
-c FILE [FILE ...], --config FILE [FILE ...]
specify configuration files
-d DATABASE, --database DATABASE
specify the database name
-f FILE, --file FILE the Python script file to execute
--histsize HISTSIZE set the number of commands to remember in the command
history
--readonly start a readonly transaction
--lock-table TABLE [TABLE ...]
lock tables
--lock-records TABLE=id,id [TABLE=id,id ...]
lock record ids from TABLE
To store changes, `transaction.commit()` must be called.
4.2 内置对象与提交规则
控制台预置三个对象:
| 对象 | 说明 |
|---|---|
pool | 模型池,pool.get('party.party') 取模型类 |
transaction | 当前事务,写入后必须 transaction.commit() 才落库 |
context | 请求上下文(语言、时区等) |
不加参数进入交互式 REPL;-f 执行脚本文件后退出;--readonly 开只读事务(防误写);--lock-table / --lock-records 用于并发安全的批量作业。
4.3 实测:脚本化查询与创建
准备脚本 /tmp/console_demo.py:
Party = pool.get('party.party')
print('PARTY_COUNT', Party.search_count([]))
User = pool.get('res.user')
admin = User(2)
print('ADMIN', admin.login, admin.email)
p = Party(name='Quzuo Demo Ltd')
p.save()
transaction.commit()
print('CREATED_ID', p.id, p.name)
print('PARTY_COUNT_AFTER', Party.search_count([]))
执行:
trytond-console -c /etc/tryton/trytond.conf -d trytondb -f /tmp/console_demo.py
实测输出:
PARTY_COUNT 1
ADMIN admin admin@example.com
CREATED_ID 34 Quzuo Demo Ltd
PARTY_COUNT_AFTER 2
查询到既有 1 条 Party,admin 账号为 admin@example.com;新建「Quzuo Demo Ltd」并 commit 后落库(ID 34),总数变 2。漏掉 transaction.commit() 的话脚本结束事务回滚,写入全部丢失——这是 console 使用中最常见的坑。
五、trytond-cron:计划任务调度器
执行模块注册的定时任务(如库存重算、报表生成、邮件发送)。
5.1 完整帮助
trytond-cron --help
实测输出:
usage: trytond-cron [-h] [--version] [-c FILE [FILE ...]] [-v] [--dev]
[-d DATABASE [DATABASE ...]] [--logconf FILE]
[--pidfile FILE] [--coroutine] [-n PROCESSES]
[--max MAXTASKSPERCHILD] [-1]
options:
-h, --help show this help message and exit
--version show program's version number and exit
-c FILE [FILE ...], --config FILE [FILE ...]
specify configuration files
-v, --verbose increase verbosity
--dev enable development mode
-d DATABASE [DATABASE ...], --database DATABASE [DATABASE ...]
specify the database names
--logconf FILE set logging configuration file (ConfigParser format)
--pidfile FILE set file to store the process id
--coroutine use coroutine for concurrency
-n PROCESSES set number of processes to use
--max MAXTASKSPERCHILD
set number of tasks a worker process before being
replaced
-1, --once run pending tasks and halt
5.2 手动触发一次(排障常用)
trytond-cron -c /etc/tryton/trytond.conf -d trytondb --once
实测输出(仅一条与本配置相关的 URI 提示,无任务待执行即安静退出):
... WARNING py.warnings ... UserWarning: The path specified in the URI will be overridden
warnings.warn("The path specified in the URI will be overridden")
退出码 0 即正常。-v 可打印每个任务的执行明细。
5.3 常驻运行
生产环境去掉 --once 即为常驻调度进程,与 trytond 服务进程相互独立,同样建议 systemd 托管(单元文件参照安装篇,把 ExecStart 换成 trytond-cron -c /etc/tryton/trytond.conf -d trytondb)。
六、常见问题
Q1:四个命令都报 command not found?
pip 安装路径未进 PATH。确认 /usr/local/bin 在 PATH 中,或用全路径 /usr/local/bin/trytond-admin。
Q2:trytond-console 里写入的数据查询不到?
漏了 transaction.commit()(见 4.3)。只读查询不受影响。
Q3:trytond-admin 执行时服务需要停吗?
初始化/激活模块会改表结构,建议先停 trytond 服务再执行,完成后再启动;-m、--validate 等只读/轻量操作可在线执行。
Q4:如何在脚本里判断 trytond-admin 是否成功?
看退出码:成功为 0。其输出多为 WARNING 级提示(如 URI path 覆盖提醒),不能以「有无输出」判断成败。
七、参考链接
- 官方文档 - trytond 命令行:https://docs.tryton.org/projects/server/en/latest/ (查询日期 2026-10-25)
- trytond on PyPI:https://pypi.org/project/trytond/ (查询日期 2026-10-25)