From ff2f610097e9ef8277dab6fcf1d3821b551579bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 24 Dec 2024 10:23:22 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E4=BF=AE=E6=AD=A3StarAgent=E5=9C=A8Linux?= =?UTF-8?q?=E4=B8=8B=E5=AE=89=E8=A3=85=E4=BA=A7=E7=94=9F=E9=94=99=E8=AF=AF?= =?UTF-8?q?service=E6=96=87=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StarAgent/Program.cs | 69 +++++++++++++++++++------------------- StarAgent/StarAgent.csproj | 2 +- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/StarAgent/Program.cs b/StarAgent/Program.cs index 1048e2ac..87a5db1d 100644 --- a/StarAgent/Program.cs +++ b/StarAgent/Program.cs @@ -150,50 +150,51 @@ protected override void Init() set.OOMScoreAdjust = -1000; // 检查并修正旧版KillMode - FixKillMode(set); + FixSystemdService(set); } } - private void FixKillMode(SystemdSetting set) + private void FixSystemdService(SystemdSetting set) { - var servicePath = typeof(Systemd).GetValue("ServicePath") as String; - if (!servicePath.IsNullOrEmpty()) - { - var file = servicePath.CombinePath($"{set.ServiceName}.service"); - if (File.Exists(file) && !File.ReadAllText(file).Contains("KillMode")) - { - WriteLog("旧版service文件,修正KillMode"); + var servicePath = Systemd.ServicePath; + if (servicePath.IsNullOrEmpty()) return; - var exe = Process.GetCurrentProcess().MainModule.FileName; + var file = servicePath.CombinePath($"{set.ServiceName}.service"); + if (!File.Exists(file)) return; - // 兼容dotnet - var args = Environment.GetCommandLineArgs(); - if (args.Length >= 1) - { - var fileName = Path.GetFileName(exe); - if (exe.Contains(' ')) exe = $"\"{exe}\""; + var txt = File.ReadAllText(file); + if (!txt.IsNullOrEmpty() && txt.Contains("KillMode") && txt.Contains("StarAgent")) return; - var dll = args[0].GetFullPath(); - if (dll.Contains(' ')) dll = $"\"{dll}\""; + WriteLog("旧版service文件,修正KillMode"); - if (fileName.EqualIgnoreCase("dotnet", "dotnet.exe")) - exe += " " + dll; - else if (fileName.EqualIgnoreCase("mono", "mono.exe", "mono-sgen")) - exe = dll; - } + var exe = Process.GetCurrentProcess().MainModule.FileName; - var service = new ServiceModel - { - ServiceName = ServiceName, - DisplayName = DisplayName, - Description = Description, - FileName = exe, - Arguments = "-s", - }; - - Host.Install(service); - } + // 兼容dotnet + var args = Environment.GetCommandLineArgs(); + if (args.Length >= 1) + { + var fileName = Path.GetFileName(exe); + if (exe.Contains(' ')) exe = $"\"{exe}\""; + + var dll = args[0].GetFullPath(); + if (dll.Contains(' ')) dll = $"\"{dll}\""; + + if (fileName.EqualIgnoreCase("dotnet", "dotnet.exe")) + exe += " " + dll; + else if (fileName.EqualIgnoreCase("mono", "mono.exe", "mono-sgen")) + exe = dll; } + + var service = new ServiceModel + { + ServiceName = ServiceName, + DisplayName = DisplayName, + Description = Description, + FileName = exe, + Arguments = "-s", + }; + + Host.Install(service); } #endregion diff --git a/StarAgent/StarAgent.csproj b/StarAgent/StarAgent.csproj index aff824a4..1a841e14 100644 --- a/StarAgent/StarAgent.csproj +++ b/StarAgent/StarAgent.csproj @@ -47,7 +47,7 @@ - +