.NET Core:Process.Start()将<defunct>子进程留在后面

Dre*_*man 4 asp.net-core-mvc .net-core

我正在构建一个部署在CentOS 7.2上的ASP.Net Core(netcore 1.1)应用程序.

我有一个动作通过System.Diagnostics.Process调用外部进程(也是使用.net内核构建的控制台应用程序),并且在返回之前不等待它退出.

问题在于,<defunct>即使在完成执行之后,所述过程也会变为并保持不变.我不想等它退出,因为这个过程可能需要几分钟才能完成.

这是一个示例代码

//The process is writing its progress to a sqlite database using a
//previously generated guid which is used later in order to check
//the task's progress

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "/bin/sh -c \"/path/to/process/executable -args\"";
psi.UseShellExecute = true;
psi.WorkingDirectory = "/path/to/process/";
psi.RedirectStandardOutput = false;
psi.RedirectStandardError = false;
psi.RedirectStandardInput = false;

using(Process proc = new Process({ StartInfo = psi }))
{
    proc.Start();
}
Run Code Online (Sandbox Code Playgroud)

该过程开始并完成其工作.它将其特定任务的进度写入sqlite数据库.然后,我可以探测该数据库以查看进度.

一切运行正常,但我可以看到ps -ef |grep executable它被列为流程执行后,它<defunct>没有其他办法摆脱它,而不是杀死它的父进程,这是我的CoreMVC应用程序.

有没有办法在.NET Core应用程序中启动进程而不等待它退出,并强制父应用程序收获生成的<defunct>子进程?

Dre*_*man 7

我以某种方式通过允许进程引发事件来修复它:

using(Process proc = new Process(
    { 
        StartInfo = psi, 
        EnableRaisingEvents = true //Allow the process to raise events, 
                                   //which I guess triggers the reaping of 
                                   //the child process by the parent 
                                   //application
    }))
{
    proc.Start();
}
Run Code Online (Sandbox Code Playgroud)