0.前提,部署到make run那一步,还未进行pm2保活 https://linux.do/t/topic/835935
1 2 3 4 5 6 7 8 9 10 11 12 git clone https://github.com/tbphp/gpt-load.git cd gpt-load go mod tidy # 创建配置 cp .env.example .env # 登陆serv00开放端口 make run # 会打印出启动成功日志 pm2保活(可选) go build -o gpt-load-server . pm2 start ./gpt-load-server --name gpt-load
1.开始,参考下面讲的passenger保活进程, https://github.com/hkfires/Keep-Serv00-Alive
2.app.js代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 const express = require("express"); const { exec } = require("child_process"); const fs = require("fs"); const path = require("path"); const app = express(); // ================== 配置区域 ================== const port = process.env.PORT || 3000; const USER = "username"; const TARGET_PORT = "30345"; const WORK_DIR = `/home/${USER}/gpt-load`; const CMD = `./gpt-load-server`; const PROCESS_NAME = "gpt-load-server"; // 日志文件路径 (在 public_nodejs 目录下) const LOG_FILE = path.join(__dirname, 'keeper.log'); // 🛑 新增:暂停开关文件路径 (只要这个文件存在,脚本就不干活) const STOP_FILE = path.join(__dirname, 'stop.txt'); // ============================================ // 自定义日志函数:同时输出到控制台和文件 function log(message) { const time = new Date().toLocaleString(); const logMsg = `[${time}] ${message}\n`; // 1. 尝试写入文件 try { fs.appendFileSync(LOG_FILE, logMsg); } catch (e) { // 如果写文件失败,至少打印到 stderr console.error(`Log write error: ${e.message}`); } // 2. 输出到控制台 (保留标准输出) console.log(logMsg.trim()); } function keepAlive() { // ============================================================ // 🛑 新增:维护模式检测 // 如果目录下存在 stop.txt,直接跳过后续所有检查 if (fs.existsSync(STOP_FILE)) { // 为了防止日志刷屏,这里可以选择不打印,或者每次跳过时打印 // 这里设置为每次周期都记录一下,方便确认状态 log("🛑 Maintenance Mode Active (stop.txt found). Skipping checks."); return; } // ============================================================ // 1. 核心检测:查端口 (保持原有逻辑) exec(`sockstat -4 -l -P tcp | grep ${TARGET_PORT}`, (err, stdout) => { if (stdout && stdout.includes(TARGET_PORT)) { log(`Port ${TARGET_PORT} is UP. Service is healthy.`); } else { log(`Port ${TARGET_PORT} is DOWN. Initiating restart sequence...`); // 2. 清理旧进程 log(`Cleaning up any stuck '${PROCESS_NAME}' processes...`); exec(`pkill -f "${PROCESS_NAME}"`, () => { // 3. 启动新进程 log(`Starting new instance...`); // 注意:gpt-load 自身的日志依然写到它自己的 gpt-load.log 里 exec(`cd ${WORK_DIR} && ${CMD} > gpt-load.log 2>&1 &`, (startErr) => { if (startErr) log(`Start failed: ${startErr}`); else log(`Start command sent successfully.`); }); }); } }); } // 启动时立即检查 log("Keeper Service Started."); keepAlive(); // 每 60 秒检查一次 setInterval(keepAlive, 60 * 1000); // 修改:Web 访问时也能看到状态 app.get("/", (req, res) => { if (fs.existsSync(STOP_FILE)) { res.send("🛑 Service is in MAINTENANCE MODE (stop.txt detected)."); } else { res.send(`✅ Keeper is watching port ${TARGET_PORT}`); } }); app.listen(port, () => log(`Keeper listening on port ${port}`));
部分网路节点存在非标准端口阻断 的问题,导致访问502 新的app.js,转发端口,来实现纯域名访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 const express = require("express"); const { exec } = require("child_process"); const fs = require("fs"); const path = require("path"); const { createProxyMiddleware } = require("http-proxy-middleware"); // 引入代理插件 const app = express(); // ================== 配置区域 ================== const port = process.env.PORT || 3000; const USER = "username"; const TARGET_PORT = "30345"; const WORK_DIR = `/home/${USER}/gpt-load`; const CMD = `./gpt-load-server`; const PROCESS_NAME = "gpt-load-server"; // 日志文件 const LOG_FILE = path.join(__dirname, 'keeper.log'); // 暂停开关 const STOP_FILE = path.join(__dirname, 'stop.txt'); // ============================================ function log(message) { const time = new Date().toLocaleString(); const logMsg = `[${time}] ${message}\n`; try { fs.appendFileSync(LOG_FILE, logMsg); } catch (e) {} console.log(logMsg.trim()); } function keepAlive() { if (fs.existsSync(STOP_FILE)) { log("🛑 Maintenance Mode Active (stop.txt found). Skipping checks."); return; } exec(`sockstat -4 -l -P tcp | grep ${TARGET_PORT}`, (err, stdout) => { if (stdout && stdout.includes(TARGET_PORT)) { log(`Port ${TARGET_PORT} is UP. Service is healthy.`); } else { log(`Port ${TARGET_PORT} is DOWN. Initiating restart sequence...`); log(`Cleaning up any stuck '${PROCESS_NAME}' processes...`); exec(`pkill -f "${PROCESS_NAME}"`, () => { log(`Starting new instance...`); exec(`cd ${WORK_DIR} && ${CMD} > gpt-load.log 2>&1 &`, (startErr) => { if (startErr) log(`Start failed: ${startErr}`); else log(`Start command sent successfully.`); }); }); } }); } // 启动保活逻辑 log("Keeper Service Started."); keepAlive(); setInterval(keepAlive, 60 * 1000); // ================== 路由配置 ================== // 1. 特殊页面:查看保活状态 (访问 /keeper-status) app.get("/keeper-status", (req, res) => { if (fs.existsSync(STOP_FILE)) { res.send("🛑 Service is in MAINTENANCE MODE (stop.txt detected)."); } else { res.send(`✅ Keeper is watching port ${TARGET_PORT}. Service acts as Reverse Proxy.`); } }); // 2. 反向代理:把所有其他请求转发给 gpt-load (30345) // 这样你直接访问域名,就等于访问了 30345,但不用担心代理节点屏蔽端口 app.use("/", createProxyMiddleware({ target: `http://127.0.0.1:${TARGET_PORT}`, changeOrigin: true, ws: true, // 支持 WebSocket (如果 gpt-load 需要) onError: (err, req, res) => { res.status(502).send(` <h1>502 Bad Gateway</h1> <p>Keeper is running, but gpt-load-server (Port ${TARGET_PORT}) seems down or starting.</p> <p>Check <a href="/keeper-status">/keeper-status</a> for details.</p> `); } })); app.listen(port, () => log(`Keeper & Proxy listening on port ${port}`));
显示keeper.log版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 const express = require("express"); const { exec } = require("child_process"); const fs = require("fs"); const path = require("path"); const os = require("os"); const { createProxyMiddleware } = require("http-proxy-middleware"); // 引入代理插件 const app = express(); // ================== 配置区域 ================== const port = process.env.PORT || 3000; const USER = "username"; const TARGET_PORT = "30345"; const WORK_DIR = `/home/${USER}/gpt-load`; const CMD = `./gpt-load-server`; const PROCESS_NAME = "gpt-load-server"; // 系统资源配额(根据您的实际环境配置) const RESOURCE_QUOTAS = { disk: { total: 3.00 * 1024 * 1024 * 1024, // 3GB in bytes unit: 'GB' }, processes: { max: 20 }, memory: { total: 512, // MB unit: 'MB' } }; // 日志文件 const LOG_FILE = path.join(__dirname, 'keeper.log'); // 暂停开关 const STOP_FILE = path.join(__dirname, 'stop.txt'); // ============================================ function log(message) { const time = new Date().toLocaleString(); const logMsg = `[${time}] ${message}\n`; try { fs.appendFileSync(LOG_FILE, logMsg); } catch (e) {} console.log(logMsg.trim()); } // 缓存系统信息以减少重复计算 let systemInfoCache = null; let lastSystemInfoTime = 0; const CACHE_DURATION = 30000; // 30秒缓存 // 获取系统资源信息(极简版 - 仅内存监控) function getSystemInfo() { const now = Date.now(); // 如果缓存有效,直接返回 if (systemInfoCache && (now - lastSystemInfoTime) < CACHE_DURATION) { return systemInfoCache; } try { // 内存检测(极简版) let totalMem = RESOURCE_QUOTAS.memory.total * 1024 * 1024; let usedMem = 0; let quotaMemUsagePercent = '0'; // 使用进程内存统计 + 经验系数 const processMem = process.memoryUsage(); usedMem = processMem.rss; // 基于serv00环境的经验系数 const multiplier = 4.2; usedMem = Math.min(usedMem * multiplier, RESOURCE_QUOTAS.memory.total * 1024 * 1024 * 0.9); quotaMemUsagePercent = ((usedMem / 1024 / 1024 / RESOURCE_QUOTAS.memory.total) * 100).toFixed(1); const sysInfo = { timestamp: new Date().toLocaleString(), memory: { total: (totalMem / 1024 / 1024).toFixed(1) + ' MB', used: (usedMem / 1024 / 1024).toFixed(1) + ' MB', free: ((totalMem - usedMem) / 1024 / 1024).toFixed(1) + ' MB', quotaUsagePercent: quotaMemUsagePercent + '%', quota: `${(usedMem / 1024 / 1024).toFixed(1)}/${RESOURCE_QUOTAS.memory.total} ${RESOURCE_QUOTAS.memory.unit} (极简估算)` } }; // 更新缓存 systemInfoCache = sysInfo; lastSystemInfoTime = now; return sysInfo; } catch (error) { return { error: 'Memory detection failed: ' + error.message, timestamp: new Date().toLocaleString() }; } } // 读取最近的 keeper.log 内容(限制行数以节省资源) function getRecentLogs(maxLines = 50) { try { if (!fs.existsSync(LOG_FILE)) { return ['Log file not found']; } const content = fs.readFileSync(LOG_FILE, 'utf8'); const lines = content.split('\n').filter(line => line.trim() !== ''); // 返回最后 maxLines 行 return lines.slice(-maxLines); } catch (error) { return [`Error reading log: ${error.message}`]; } } function keepAlive() { if (fs.existsSync(STOP_FILE)) { log("🛑 Maintenance Mode Active (stop.txt found). Skipping checks."); return; } exec(`sockstat -4 -l -P tcp | grep ${TARGET_PORT}`, (err, stdout) => { if (stdout && stdout.includes(TARGET_PORT)) { log(`Port ${TARGET_PORT} is UP. Service is healthy.`); } else { log(`Port ${TARGET_PORT} is DOWN. Initiating restart sequence...`); log(`Cleaning up any stuck '${PROCESS_NAME}' processes...`); exec(`pkill -f "${PROCESS_NAME}"`, () => { log(`Starting new instance...`); exec(`cd ${WORK_DIR} && ${CMD} > gpt-load.log 2>&1 &`, (startErr) => { if (startErr) log(`Start failed: ${startErr}`); else log(`Start command sent successfully.`); }); }); } }); } // 启动保活逻辑 log("Keeper Service Started."); keepAlive(); setInterval(keepAlive, 60 * 1000); // ================== 路由配置 ================== // 1. 特殊页面:查看保活状态 (访问 /keeper-status) app.get("/keeper-status", (req, res) => { const sysInfo = getSystemInfo(); const logs = getRecentLogs(30); // 显示最近30行日志 let html = ` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Keeper Status Dashboard</title> <style> body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; } .container { max-width: 1200px; margin: 0 auto; } .card { background: white; border-radius: 8px; padding: 20px; margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .status-header { text-align: center; color: #333; } .status-indicator { display: inline-block; padding: 10px 20px; border-radius: 20px; font-weight: bold; margin: 10px 0; } .status-active { background-color: #d4edda; color: #155724; } .status-maintenance { background-color: #fff3cd; color: #856404; } .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; } .resource-card { background: #f8f9fa; border-left: 4px solid #007bff; } .log-container { background: #2d2d2d; color: #f8f8f2; padding: 15px; border-radius: 5px; font-family: 'Courier New', monospace; font-size: 12px; max-height: 400px; overflow-y: auto; } .log-entry { margin: 2px 0; } .log-error { color: #ff6b6b; } .log-success { color: #51cf66; } .log-warning { color: #ffd43b; } .refresh-btn { background: #007bff; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; margin: 10px 5px; } .refresh-btn:hover { background: #0056b3; } h2 { color: #333; border-bottom: 2px solid #007bff; padding-bottom: 10px; } .metric { margin: 10px 0; } .metric-label { font-weight: bold; color: #555; } .metric-value { color: #007bff; font-family: 'Courier New', monospace; } .quota-info { font-size: 12px; color: #666; margin-top: 5px; } .usage-bar { height: 8px; background-color: #e9ecef; border-radius: 4px; margin: 8px 0; overflow: hidden; } .usage-fill { height: 100%; background-color: #28a745; transition: width 0.3s ease; } .usage-warning { background-color: #ffc107; } .usage-danger { background-color: #dc3545; } </style> </head> <body> <div class="container"> <div class="card"> <h1 class="status-header">keeper Status Dashboard</h1> <div class="status-indicator ${fs.existsSync(STOP_FILE) ? 'status-maintenance' : 'status-active'}"> ${fs.existsSync(STOP_FILE) ? '🛑 MAINTENANCE MODE' : '✅ ACTIVE MODE'} </div> <p>Monitoring port: <strong>${TARGET_PORT}</strong></p> <p>Last updated: <strong>${sysInfo.timestamp}</strong></p> <button class="refresh-btn" onclick="location.reload()">Refresh Status</button> <button class="refresh-btn" onclick="window.location.href='/'">Go to Main Site</button> </div> `; // 系统资源信息(仅显示内存) if (!sysInfo.error) { html += ` <div class="grid"> <div class="card resource-card"> <h2>💾 Memory Information</h2> <div class="metric"> <span class="metric-label">Total Memory:</span> <span class="metric-value">${sysInfo.memory.total}</span> </div> <div class="metric"> <span class="metric-label">Used Memory:</span> <span class="metric-value">${sysInfo.memory.used}</span> </div> <div class="metric"> <span class="metric-label">Free Memory:</span> <span class="metric-value">${sysInfo.memory.free}</span> </div> <div class="metric"> <span class="metric-label">Quota Usage:</span> <span class="metric-value">${sysInfo.memory.quotaUsagePercent}</span> <div class="quota-info">Quota: ${sysInfo.memory.quota}</div> </div> <div class="usage-bar"> <div class="usage-fill ${parseFloat(sysInfo.memory.quotaUsagePercent) > 80 ? 'usage-warning' : parseFloat(sysInfo.memory.quotaUsagePercent) > 90 ? 'usage-danger' : ''}" style="width: ${parseFloat(sysInfo.memory.quotaUsagePercent)}%"></div> </div> </div> </div> `; } else { html += ` <div class="card"> <h2>⚠️ Memory Information Error</h2> <p>${sysInfo.error}</p> </div> `; } // 日志显示 html += ` <div class="card"> <h2>📝 Recent Keeper Logs</h2> <div class="log-container"> `; logs.forEach(logEntry => { if (logEntry.trim()) { // 为不同类型的日志添加颜色 let logClass = 'log-entry'; if (logEntry.includes('ERROR') || logEntry.includes('FAILED')) { logClass += ' log-error'; } else if (logEntry.includes('SUCCESS') || logEntry.includes('UP') || logEntry.includes('healthy')) { logClass += ' log-success'; } else if (logEntry.includes('MAINTENANCE') || logEntry.includes('STOP')) { logClass += ' log-warning'; } html += `<div class="${logClass}">${logEntry}</div>`; } }); html += ` </div> </div> </div> </body> </html> `; res.send(html); }); // 2. 反向代理:把所有其他请求转发给 gpt-load (30345) // 这样你直接访问域名,就等于访问了 30345,但不用担心代理节点屏蔽端口 app.use("/", createProxyMiddleware({ target: `http://127.0.0.1:${TARGET_PORT}`, changeOrigin: true, ws: true, // 支持 WebSocket (如果 gpt-load 需要) onError: (err, req, res) => { res.status(502).send(` <h1>502 Bad Gateway</h1> <p>Keeper is running, but gpt-load-server (Port ${TARGET_PORT}) seems down or starting.</p> <p>Check <a href="/keeper-status">/keeper-status</a> for details.</p> `); } })); app.listen(port, () => log(`Keeper & Proxy listening on port ${port}`));
### 3.启动程序(要以绝对路径启动,不然……app.js没办法重启gpt-load-server)启动gpt-load-server
1 2 cd /home/username/gpt-load nohup ./gpt-load-server > gpt-load.log 2>&1 &
启动app.js(在serv00网站restart就行,会自动生成keeper.log )
```cd /home/username/domains/username.serv00.net/public_nodejsnohup node app.js > app.log 2>&1 &```
4.验证 手动杀掉gpt-load-server
1 pkill -f gpt-load-server
在public_nodejs目录
1 2 [id@s9]:<~/domains/id.serv00.net/public_nodejs>$ pkill -f gpt-load-server [id@s9]:<~/domains/id.serv00.net/public_nodejs>$ tail -f keeper.log
查看app.log keeper.log
显示下面则成功:
5.暂停保活(开启维护模式): 进入app.js所在目录创建 stop.txt 文件:
恢复保活(关闭维护模式): 删除 stop.txt 文件:
PS:最简单的还是pm2保活,官方也没看到说禁止,说的是最好用passenger,访问我用的域名+端口,可以自己试试passenger反代去掉端口