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

huangapple go评论76阅读模式
英文:

Don't kill process when dotnet project shut down

问题

我正在为Linux系统(Debian)开发一个.NET应用程序。我想从我的应用程序上运行Linux命令。我正在使用以下方法运行命令。如果可能的话,我真的不想改变它。

public static async Task<Process> RunCommandAsync(string command)
{
    string cmd = command.Replace("&quot;", "\\&quot;");
    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软件包中,并且同时运行两个应用程序,都作为服务。在某个时刻,我想卸载该应用程序,这会停止两个服务。我首先停止负责此操作的服务,然后另一个服务永远不会停止,应用程序无法正确卸载。

英文:

I'm working on a dotnet app for a Linux system (debian). I want to run commands on Linux from my app. I'm running commands using this method. I really don't want to change it if possible.

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

My goal is to start a process within the project and not killing it when I shut down the project. I wanted to use only a command. I tried something like this sudo nohup sh -c &#39;ping google.com&#39; &gt; /dev/null &amp;, which I though that would work but when I shut down the project the process is also terminated.

In short, in this example I wanted to continuing the ping even after I shut down the project.

Edit 1

Here, for simplicity, I gave the example of a ping. In reality the app is in a debian package and there are two apps running at the same time, both as services. At some point I want in uninstall the app, which stops both services. I stop the service responsible for this first, and then the other service is never stop and the app isn't uninstalled correctly.

答案1

得分: 0

我找到了一个解决方案。我创建了一个服务(finish.service),如下所示:

[Unit]
Description=卸载 app1 并安装 app2

[Service]
Type=oneshot
ExecStart=/opt/finish/finish.sh
WorkingDirectory=/opt/finish/
StandardOutput=journal
StandardError=journal

其中 "/opt/finish/finish.sh"(简化的代码):

#!/bin/bash

sudo apt purge app1;
sudo apt install app2;

在我的应用程序中,当我想要卸载它并安装一个新应用时,我只需调用:

sudo systemctl start finish.service

英文:

I found a solution. I created a service (finish.service) like this:

[Unit]
Description=Uninstall app1 and install app2  

[Service]
Type=oneshot
ExecStart=/opt/finish/finish.sh
WorkingDirectory=/opt/finish/
StandardOutput=journal
StandardError=journal

Where "/opt/finish/finish.sh": (simplified code)

#!/bin/bash

sudo apt purge app1;
sudo apt install app2;

In my app, when I want to uninstall it and install a new one I simply call

sudo systemctl start finish.service

huangapple
  • 本文由 发表于 2023年7月24日 17:51:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定