diff --git a/src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs b/src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs index bee0e63..1dd322e 100644 --- a/src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs +++ b/src/UScheduler/BackgroundServices/PSScriptBackgroundService.cs @@ -51,10 +51,13 @@ namespace UScheduler.BackgroundServices { // When the stopping token is canceled, for example, a call made from services.msc, // we shouldn't exit with a non-zero exit code. In other words, this is expected... _logger.LogInformation("Stopping PSScriptBackgroundService due to cancellation request"); + _psScriptService.TerminateAllScripts(); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); + _psScriptService.TerminateAllScripts(); + // Terminates this process and returns an exit code to the operating system. // This is required to avoid the 'BackgroundServiceExceptionBehavior', which // performs one of two scenarios: diff --git a/src/UScheduler/BackgroundServices/ProcessBackgroundService.cs b/src/UScheduler/BackgroundServices/ProcessBackgroundService.cs index 5037542..02c8036 100644 --- a/src/UScheduler/BackgroundServices/ProcessBackgroundService.cs +++ b/src/UScheduler/BackgroundServices/ProcessBackgroundService.cs @@ -55,10 +55,13 @@ public sealed class ProcessBackgroundService : BackgroundService { // When the stopping token is canceled, for example, a call made from services.msc, // we shouldn't exit with a non-zero exit code. In other words, this is expected... _logger.LogInformation("Stopping ProcessBackgroundService due to cancellation request"); + _processService.TerminateAllProcesses(); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); + _processService.TerminateAllProcesses(); + // Terminates this process and returns an exit code to the operating system. // This is required to avoid the 'BackgroundServiceExceptionBehavior', which // performs one of two scenarios: