MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / getCurrentServiceName

Function getCurrentServiceName

service/service.ts:55–110  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

53 Sun: "周日",
54};
55
56// 自动检测当前进程对应的systemd服务名称
57async function getCurrentServiceName(): Promise<string> {
58 try {
59 const currentPid = String(process.pid);
60
61 // 方法1:通过当前进程PID获取服务名称
62 try {
63 const { stdout } = await execFileAsync("ps", ["-o", "unit=", "-p", currentPid]);
64 if (stdout && stdout.trim() && !stdout.trim().startsWith("-")) {
65 const unitName = stdout.trim();
66 if (unitName.endsWith(".service")) {
67 return unitName.slice(0, -8); // 移除.service后缀
68 }
69 return unitName;
70 }
71 } catch (error) {
72 // 忽略错误,继续尝试其他方法
73 }
74
75 // 方法2:使用systemctl status PID
76 try {
77 const { stdout } = await execFileAsync("systemctl", ["status", currentPid]);
78 if (stdout) {
79 // 提取服务名称,格式类似: ● service-name.service - Description
80 const match = stdout.match(/[●◯]\s*([^.\\s]+)/);
81 if (match) {
82 return match[1];
83 }
84 }
85 } catch (error) {
86 // 忽略错误,继续尝试其他方法
87 }
88
89 // 方法3:通过进程名称猜测(回退方案)
90 try {
91 // 检查常见的pagermaid服务名称
92 const commonNames = ["pagermaid", "pgm", "pagermaid-modify", "pgm-sg", "pgm-hk"];
93 for (const name of commonNames) {
94 try {
95 const { stdout } = await execFileAsync("systemctl", ["is-active", name]);
96 if (stdout && stdout.includes("active")) {
97 return name;
98 }
99 } catch (error) {
100 // 继续检查下一个
101 }
102 }
103 } catch (error) {
104 // 忽略错误
105 }
106 } catch (error) {
107 console.error("Error detecting service name:", error);
108 }
109
110 // 如果所有方法都失败,返回默认值
111 return "pagermaid";
112}

Callers 1

handleServiceRequestFunction · 0.85

Calls 2

execFileAsyncFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected