Skip to content

Commit

Permalink
[fix]修正StarAgent在Linux下安装产生错误service文件的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 24, 2024
1 parent 27d9a75 commit ff2f610
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
69 changes: 35 additions & 34 deletions StarAgent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion StarAgent/StarAgent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.Agent" Version="10.10.2024.1219" />
<PackageReference Include="NewLife.Agent" Version="10.10.2024.1223" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit ff2f610

Please sign in to comment.