Search code examples
c#windows-servicesnssm

Child process is killed when windows service is stopped


I have a small C# console app that monitors some configured process(es)/app(s), these are also C# console apps, and keep them running so it starts them if they crashed. It works well if I run this monitor console app by executing exe file (in interactive mode). However if I install this app as a windows service using NSSM, the monitored process(es) are killed when the service is stopped which is not what I want.

The minimum code to reproduce (this code starts the child process only once and does not monitor it at all):

Monitoring app:

class Program
{
    static void Main(string[] args)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();

        startInfo.FileName = "..\\..\\..\\TestApp\\bin\\debug\\TestApp.exe";            

        Process.Start(startInfo);

        Console.WriteLine("Process started");

        Console.Read();
    }
}

Monitored app:

static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("running...");

            Thread.Sleep(3000);
        }
    }

Is there a way how to keep these child process(es) running after windows service is stopped?

Thanks


Solution

  • NSSM has a configuration option which does not kill the entire process tree when stopping the service.

    You can configure this in the UI with nssm.exe edit "my service name" or directly with nssm.exe set "my service name" AppKillProcessTree 0.