详细分析 ▾
运行时依赖
版本
Universal-autostart v1.0.0 – 首个跨平台自启动服务管理器,支持 Windows 与 macOS。 - 安装、卸载、启动、停止及监控后台服务 - 服务崩溃后自动重启 - 借助 sc/schtasks(Windows)或 launchd(macOS)实现重启后仍保持的持久化服务配置
安装命令
点击复制技能文档
# Universal AutoStart Service Manager v1.1 跨平台自启动服务管理器,支持 Windows 和 macOS。可安装、卸载、启动、停止、监控服务,并支持自动重启。 ## 🚀 快速开始 ### Windows 安装 创建 install_windows.bat(纯文本,UTF-8 保存): ``batch @echo off chcp 65001 >nul echo ============================================================ echo 通用自启动服务 - 安装工具 v1.1 echo ============================================================ :: 检查管理员权限 net session >nul 2>&1 if %errorLevel% neq 0 ( echo [ERROR] 请以管理员身份运行! pause exit /b 1 ) echo [OK] 已获取管理员权限 :: 查找配置文件 set CONFIG_FILE=%~dp0service_config.json if not exist "%CONFIG_FILE%" ( set CONFIG_FILE=%~dp0qwenpaw_service_config.json ) if not exist "%CONFIG_FILE%" ( echo [ERROR] 未找到配置文件! pause exit /b 1 ) :: 安装服务 python "%~dp0universal_service.py" install "%CONFIG_FILE%" --no-check-admin if %errorLevel% equ 0 ( echo [OK] 安装完成!服务将在下次开机自动启动 ) else ( echo [ERROR] 安装失败! ) pause ` 右键 install_windows.bat → 以管理员身份运行 ### macOS 安装 使用自带 install_macos.sh: `bash sudo ./install_macos.sh ` ## 📋 核心功能 | 功能 | 描述 | |------|------| | 跨平台 | Windows (sc + schtasks)、macOS (launchd) | | 自动重启 | 崩溃后自动重启(可设最大次数) | | 日志 | 实时输出,支持轮转 | | 健康检查 | 端口/进程检测,确保存活 | | 优雅退出 | 支持 SIGTERM/SIGINT | | 环境变量 | 支持加载 .env | ## 🔧 用法 ### 命令行 `bash # 安装自启动(不立即运行) python universal_service.py install [config.json] [--no-check-admin] # 卸载自启动 python universal_service.py uninstall [config.json] [--no-check-admin] # 手动启动 python universal_service.py start [config.json] # 停止 python universal_service.py stop [config.json] # 查看状态 python universal_service.py status [config.json] # 直接运行(带自启动) python universal_service.py [config.json] ` ### 双击脚本 - Windows: install.bat / uninstall.bat - macOS: install_macos.sh / uninstall_macos.sh ## ⚙️ 配置 ### QwenPaw 标准 (qwenpaw_service_config.json) `json { "service_name": "QwenPawService", "display_name": "QwenPaw 智能助手服务", "program": { "type": "python", "path": "python", "arguments": "-m qwenpaw.cli", "working_dir": "C:/Users/Administrator/.copaw/workspaces/default" }, "environment": { "load_dotenv": true, "variables": {} }, "log": { "enabled": true, "level": "INFO", "dir": ".logs", "max_size_mb": 10, "backup_count": 5, "console": true }, "health_check": { "enabled": true, "type": "port", "port": 8765, "interval_seconds": 30, "timeout_seconds": 5, "max_failures": 3 }, "restart": { "auto_restart": true, "max_restarts": 5, "restart_delay": 30 } } ` ### 通用示例 (service_config.example.json) `json { "service_name": "MyCustomService", "display_name": "我的自定义服务", "program": { "type": "python", "path": "python3", "arguments": "app.py", "working_dir": "/path/to/app" }, "environment": { "load_dotenv": false, "variables": { "NODE_ENV": "production" } }, "log": { "enabled": true, "level": "DEBUG", "dir": "./logs" }, "health_check": { "enabled": true, "type": "port", "port": 3000, "interval_seconds": 10 }, "restart": { "auto_restart": true, "max_restarts": 3, "restart_delay": 60 } } ` ### 字段说明 | 字段 | 类型 | 说明 | |------|------|------| | service_name | string | 服务内部唯一名 | | display_name | string | 显示名 | | program.type | string | 类型:python / node / binary / shell | | program.path | string | 可执行文件路径 | | program.arguments | string | 启动参数 | | program.working_dir | string | 工作目录 | | environment.load_dotenv | bool | 是否加载 .env | | environment.variables | object | 额外环境变量 | | log.enabled | bool | 是否启用日志 | | log.level | string | 级别:DEBUG/INFO/WARNING/ERROR | | log.dir | string | 日志目录 | | log.max_size_mb | int | 单文件上限 (MB) | | log.backup_count | int | 保留份数 | | log.console | bool | 是否输出控制台 | | health_check.enabled | bool | 是否开启健康检查 | | health_check.type | string | 类型:port / process | | health_check.port | int | 端口号 | | health_check.interval_seconds | int | 检查间隔 | | health_check.timeout_seconds | int | 超时 | | health_check.max_failures | int | 最大失败次数 | | restart.auto_restart | bool | 是否自动重启 | | restart.max_restarts` | int