2023-08-21 09:26:31 +08:00
|
|
|
|
import fs from "node:fs"
|
|
|
|
|
import childProcess from "child_process"
|
|
|
|
|
|
|
|
|
|
const _path = process.cwd()
|
|
|
|
|
|
2024-06-09 04:00:28 +08:00
|
|
|
|
fs.readFile(`${_path}/config/pm2/pm2.json`, "utf8", (err, data) => {
|
2023-08-21 09:26:31 +08:00
|
|
|
|
if (err) {
|
2024-06-09 04:00:28 +08:00
|
|
|
|
console.error("pm2.json文件读取错误:", err)
|
2023-08-21 09:26:31 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const config = JSON.parse(data)
|
|
|
|
|
if (config.apps && config.apps.length > 0 && config.apps[0].name) {
|
|
|
|
|
const appName = config.apps[0].name
|
|
|
|
|
runPm2Logs(appName)
|
|
|
|
|
} else {
|
2024-06-09 04:00:28 +08:00
|
|
|
|
console.error("读取失败:无法在pm2.json中找到name数组")
|
2023-08-21 09:26:31 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (parseError) {
|
2024-06-09 04:00:28 +08:00
|
|
|
|
console.error("读取失败:json文件解析发生了错误", parseError)
|
2023-08-21 09:26:31 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function runPm2Logs(appName) {
|
2024-06-09 04:00:28 +08:00
|
|
|
|
const command = process.platform === "win32" ? "pm2.cmd" : "pm2"
|
|
|
|
|
const args = ["logs", "--lines", "400", appName]
|
|
|
|
|
|
2024-06-09 04:03:50 +08:00
|
|
|
|
// console.log(`Command: ${command}`)
|
|
|
|
|
// console.log(`Args: ${args.join(" ")}`)
|
2024-06-09 04:00:28 +08:00
|
|
|
|
|
|
|
|
|
const pm2LogsProcess = childProcess.spawn(command, args, { stdio: "inherit", shell: true })
|
|
|
|
|
|
|
|
|
|
pm2LogsProcess.on("error", (error) => {
|
|
|
|
|
console.error("Error spawning pm2 logs process:", error)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
pm2LogsProcess.on("exit", (code) => {
|
2023-08-21 09:26:31 +08:00
|
|
|
|
if (code !== 0) {
|
|
|
|
|
console.error(`pm2 logs process exited with code ${code}`)
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-10-25 03:34:37 +08:00
|
|
|
|
}
|