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 const express = require("express"); const path = require("path"); const exec = require("child_process").exec; const app = express(); const port = port;//前提里面设置的端口 const user = ""; // Serv00 用户名 const pName = "gpt-load-server"; // 我保活了这个 // 完整路径到二进制程序(注意:不需要 go run,直接运行编译后的文件) //const Process = `/home/${user}/gpt-load/${pName}`;//这里我手动kill了gpt-load-server之后没有触发app.js里面重启gpt-load-server的逻辑 const Process = "gpt-load/gpt-load-server"; // 匹配路径中包含这一段 // 日志文件路径 const LogFile = `/home/${user}/gpt-load/gpt-load.log`; function keepWebAlive() { const currentDate = new Date(); const formattedDate = currentDate.toLocaleDateString(); const formattedTime = currentDate.toLocaleTimeString(); // 精确查找该进程是否在运行 exec(`pgrep -f "${Process}"`, (err, stdout) => { if (stdout.trim()) { console.log(`${formattedDate}, ${formattedTime}: ${pName} is Running (PID: ${stdout.trim()})`); } else { console.log(`${formattedDate}, ${formattedTime}: ${pName} is NOT Running, attempting to restart...`); // 使用 nohup 启动,输出日志 exec(`cd /home/${user}/gpt-load && nohup ${Process} > ${LogFile} 2>&1 &`, (err) => { if (err) { console.error(`${formattedDate}, ${formattedTime}: Restart failed: ${err}`); } else { console.log(`${formattedDate}, ${formattedTime}: Restarted ${pName} successfully!`); } }); } }); } // 初始执行一次 keepWebAlive(); // 每 10 秒检查一次 setInterval(keepWebAlive, 10 * 1000); app.use(express.static(path.join(__dirname, 'static'))); app.listen(port, () => { console.log(`Web server is 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
1 2 cd /home/username/domains/username.serv00.net/public_nodejs nohup 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 app.log
查看app.log
显示下面则成功:
5.serv00母鸡重启的话,需要重启app.js 写一个start.sh脚本
1 2 3 4 5 6 #!/bin/bash cd /home/username/gpt-load nohup ./gpt-load-server > gpt-load.log 2>&1 & cd /home/username/domains/username.serv00.net/public_nodejs nohup node app.js > app.log 2>&1 &
赋权并运行
1 2 chmod +x start.sh ./start.sh
之后登陆则运行
即可
PS:最简单的还是pm2保活,官方也没看到说禁止,说的是最好用passenger,访问我用的域名+端口,可以自己试试passenger反代去掉端口