Quellcode durchsuchen

新增启动服务的时候计算当前pid,用于以后关闭服务使用

mafei vor 1 Jahr
Ursprung
Commit
c171bc3939
1 geänderte Dateien mit 34 neuen und 0 gelöschten Zeilen
  1. 34 0
      script/Main.lua

+ 34 - 0
script/Main.lua

@@ -1,4 +1,38 @@
 
+ -- 使用os_execute执行系统命令获取PID
+local function get_pid()
+    local handle = io.popen("ps aux | grep ./logic | grep -v grep | awk '{print $2}'", "r")
+    local pid = handle:read("*a")
+    handle:close()
+    return tonumber(pid)
+end
+ 
+-- 在Windows上获取PID的另一种方法
+local function get_pid_windows()
+    local handle = io.popen("echo %PROCESS_ID%", "r")
+    local pid = handle:read("*a")
+    handle:close()
+    return tonumber(pid)
+end
+ 
+-- 根据操作系统选择正确的函数
+local get_pid_func
+if jit.os == "Windows" then
+    get_pid_func = get_pid_windows
+else
+    get_pid_func = get_pid
+end
+
+local function createPidFile ()
+    local pid = get_pid_func()
+    local f,err = io.open("logicPidFile","w")
+    if not f then 
+        print("open file failed",err)
+        return
+    end
+    f:write(pid)
+end
+
  if jit then
   jit.off()
   jit.flush()