dotnet项目关闭时不要终止进程

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

我正在为 Linux 系统(debian)开发一个 dotnet 应用程序。我想从我的应用程序在 Linux 上运行命令。我正在使用这种方法运行命令。如果可以的话我真的不想改变。

public static async Task<Process> RunCommandAsync(string command)
{
    string cmd = command.Replace("\"", "\\\"");
    Process process = new()
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "/bin/bash",
            Arguments = $"-c \"{cmd}\"",
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true,
        }
    };
    process.Start();
    await process.WaitForExitAsync();
    return process;
}

我的目标是在项目内启动一个进程,而不是在关闭项目时杀死它。我只想使用一个命令。我尝试了这样的方法

sudo nohup sh -c 'ping google.com' > /dev/null &
,我认为这会起作用,但是当我关闭项目时,该过程也终止了。

简而言之,在这个例子中,即使我关闭了项目,我也想继续执行 ping 操作。

编辑1

这里为了简单起见,我举了 ping 的例子。实际上,该应用程序位于 debian 软件包中,并且有两个应用程序同时运行,两者都作为服务。在某些时候,我想卸载该应用程序,这会停止这两项服务。我首先停止负责此操作的服务,然后其他服务永远不会停止,并且该应用程序未正确卸载。

c# .net linux process parent-child
© www.soinside.com 2019 - 2024. All rights reserved.