如何告诉systemd服务处于“活动(退出)”状态?

问题描述 投票:0回答:1

我有一个控制台程序,它启动一个无限循环的后台线程。

private static ManualResetEventSlim _quitProgramEvent = new();

private static void Main(string[] args)
{
    PosixSignalRegistration.Create(PosixSignal.SIGTERM, c => _quitProgramEvent.Set());

    Thread t = new(InfiniteLoop);
    t.IsBackground = true;
    t.Start();

    _quitProgramEvent.Wait();
}

该程序作为 systemd 服务执行。

[Unit]
Description=xreen
Requires=network.target
After=network.target

[Service]
ExecStart=/path/to/console-programm
WorkingDirectory=/path/to/dir
Type=oneshot

当我通过

service xreen status
检查状态时,它告诉我该服务仍在“激活”。

Active:自 2024-04-07 周日 10:39:54 CEST 开始激活(开始); 58分钟

如何告诉 systemd 该服务已经启动并正在运行。

如何在C#中从控制台程序更改状态?

c# .net service systemd
1个回答
0
投票

我在这里找到了解决方案: https://github.com/aremmell/net-core-systemd-service#2-notify-systemd-upon-successful-initialization

public static class Systemd
{
    [DllImport("libsystemd.so.0")]
    public static extern int sd_notify(int unset_environment, string state);
}

// Call it like this
Systemd.sd_notify(0, "READY=1");
© www.soinside.com 2019 - 2024. All rights reserved.