A FastAPI based wrapper to auto-run and API-restart any terminal command.
Project description
FastAPI Process Restarter
一个基于 FastAPI 的通用进程管理和重启工具,支持跨平台(Linux/macOS/Windows)。
功能特性
- 🚀 一键重启: 通过 HTTP API 重启任何命令/脚本
- 🔄 自动重启: 进程崩溃后自动恢复(v0.2.0+)
- 🖥️ 跨平台支持: 原生兼容 Linux、macOS 和 Windows
- 📊 状态监控: 实时查看进程运行状态和 PID
- 🧩 易于集成: 可作为子应用挂载到现有 FastAPI 项目
- 🔧 CLI 工具: 提供命令行工具,开箱即用
安装
pip install fastapi-process-restarter
使用方法
方式一:命令行工具 (CLI)
安装后,直接在终端使用 restarter 命令:
# 基本用法
restarter --cmd "python main.py"
# 指定端口
restarter --cmd "python main.py" --port 9000
# 启用自动重启(进程崩溃后自动恢复)
restarter --cmd "python main.py" -a
# 自定义检查间隔(默认 3 秒)
restarter --cmd "python main.py" -a --check-interval 5.0
# 完整参数示例
restarter -c "node server.js" --host 127.0.0.1 -p 8080 -a --check-interval 2.0
CLI 参数说明:
| 参数 | 简写 | 说明 | 默认值 |
|---|---|---|---|
--cmd |
-c |
要运行的命令(必需) | - |
--host |
- | API 监听地址 | 0.0.0.0 |
--port |
-p |
API 端口 | 8080 |
--auto-restart |
-a |
进程崩溃后自动重启 | False |
--check-interval |
- | 自动重启检查间隔(秒) | 3.0 |
API 端点:
POST /restart- 手动重启进程GET /status- 获取进程状态(包含 auto_restart 配置信息)
示例:
# 启动带自动重启的服务
restarter --cmd "python main.py" -a --check-interval 3.0
# 手动重启进程
curl -X POST http://localhost:8080/restart
# 查看状态
curl http://localhost:8080/status
# 输出示例:
# {"running":true,"pid":12345,"auto_restart":true,"check_interval":3.0}
方式二:Python API 集成
在现有 FastAPI 项目中作为子应用挂载:
from fastapi import FastAPI
from process_restarter import create_app
# 你的主应用
main_app = FastAPI()
@main_app.get("/")
def home():
return {"message": "Hello World"}
# 挂载进程重启器(启用自动重启)
cmd_app = create_app(
cmd="node server.js",
auto_restart=True, # 启用自动重启
check_interval=5.0 # 每 5 秒检查一次
)
main_app.mount("/subsystem", cmd_app)
# 现在可以通过以下端点操作:
# POST /subsystem/restart - 手动重启 node server.js
# GET /subsystem/status - 查看进程状态
也可以直接使用 ProcessManager:
from process_restarter import ProcessManager
# 创建管理器(启用自动重启)
manager = ProcessManager(
cmd="python worker.py",
auto_restart=True,
check_interval=3.0
)
manager.start() # 启动进程(同时启动监控线程)
manager.restart() # 手动重启进程
manager.stop() # 停止进程和监控线程
技术细节
- 跨平台进程管理:
- Linux/macOS: 使用
os.setsid创建进程组 - Windows: 使用
CREATE_NEW_PROCESS_GROUP创建进程组
- Linux/macOS: 使用
- 自动重启机制:
- 独立守护线程每 N 秒检查进程状态
- 进程退出时自动重新启动
- 支持自定义检查间隔
- 生命周期管理:使用 FastAPI
@asynccontextmanager确保程序退出时子进程被清理 - 日志桥接:子进程输出实时转发到主进程 stdout
版本历史
v0.2.0
- 新增自动重启功能 (
--auto-restart,-a) - 新增检查间隔配置 (
--check-interval) /status接口返回自动重启配置信息
v0.1.0
- 初始版本
- 进程启动/停止/重启功能
- FastAPI 接口支持
- CLI 命令行工具
许可证
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_process_restarter-0.2.1.tar.gz.
File metadata
- Download URL: fastapi_process_restarter-0.2.1.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a15177a6a869f03de79f0429acd04866f8ce2a171e1a2282c3c8714660c11677
|
|
| MD5 |
8999b3084056eacbbb10e4e05d0f4c0f
|
|
| BLAKE2b-256 |
36adba4946163ebfbbdee1a377db7c62b36eb46e88530bc3995451f67389a104
|
File details
Details for the file fastapi_process_restarter-0.2.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_process_restarter-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87f21f9c1b0584bf1f19d3d238c4cf07b3f4ad560c3852e6ed12e52f0d66cddb
|
|
| MD5 |
da6465beacc6455dbab00c93d765a8d2
|
|
| BLAKE2b-256 |
4c66d212d5cbe0d7e4fece91bd94a00bbd733b6b51b55a60ecf433a0932e81ec
|