2017-04-20 77 views
2

我正在構建部署在CentOS 7.2上的ASP.Net Core(netcore 1.1)應用程序。.NET Core:Process.Start()離開<defunct>子進程

我有一個操作,它通過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(); 
} 

的過程開始,它的工作。它將其特定任務的進度寫入sqlite數據庫。然後,我可以探查該數據庫以檢查進度。

一切運行正常,但我可以看到在ps -ef |grep executable過程執行後,它被列爲<defunct>,我沒有其他辦法擺脫它,而不是殺死它的父進程,這是我的CoreMVC應用程序。

有沒有辦法在.NET Core應用程序中啓動進程而無需等待它退出,並強制父應用程序收穫得到的<defunct>子進程?

回答

4

我莫名其妙地通過允許過程引發事件固定它:

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(); 
} 
+1

謝謝夥計! –