.NET Core: Process.Start () leaving an <Untitled> child process behind
I am creating an ASP.Net Core (netcore 1.1) application deployed on CentOS 7.2.
I have an activity that calls an external process (a console application, also embedded with a .net core) via System.Diagnostics.Process and without waiting for it to exit before returning.
The problem is that the specified process becomes and remains <defunct>
even after it ends. I don't want to wait for it to come out because this process can take a few minutes to get the job done.
Here is some sample code
//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 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(); }
The process starts and does its job. He writes about his progress on his specific task for sqlite database. Then I can check this database to check the progress.
Everything works fine, but after executing the process with ps -ef |grep executable
I see that it is listed as <defunct>
and I have no other way to get rid of it other than to kill its parent process which is my CoreMVC application.
Is there a way to start a process in a .NET Core app without waiting for it to exit and force the parent app to get the resulting child process <defunct>
?
source share
I fixed this somehow by allowing the process to raise events:
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(); }
source share