@@ -25,6 +25,7 @@ public class SSHService : IHostedService, INotificationHandler<ConfigurationChan
25
25
private readonly IMediator _mediator ;
26
26
private bool _loggingIgnoreNoIdentificationString ;
27
27
private Process _serverProcess ;
28
+ private Action _serviceProcessExitAction ;
28
29
29
30
30
31
public SSHService ( ILogger < SSHService > logger , IMediator mediator )
@@ -163,6 +164,21 @@ private async Task UpdateHostKeyFiles()
163
164
private async Task StartOpenSSH ( )
164
165
{
165
166
_logger . LogInformation ( "Starting 'sshd' process" ) ;
167
+ _serviceProcessExitAction = ( ) =>
168
+ {
169
+ _logger . LogWarning ( "'sshd' process has stopped. Restarting process." ) ;
170
+ RestartService ( ) . Wait ( ) ;
171
+ } ;
172
+
173
+ void ListenForExit ( )
174
+ {
175
+ //Use this approach since the Exited event does not trigger on process crash
176
+ Task . Run ( ( ) =>
177
+ {
178
+ _serverProcess . WaitForExit ( ) ;
179
+ _serviceProcessExitAction ? . Invoke ( ) ;
180
+ } ) ;
181
+ }
166
182
_serverProcess = new Process
167
183
{
168
184
StartInfo =
@@ -180,6 +196,7 @@ private async Task StartOpenSSH()
180
196
_serverProcess . OutputDataReceived += OnSSHOutput ;
181
197
_serverProcess . ErrorDataReceived += OnSSHOutput ;
182
198
_serverProcess . Start ( ) ;
199
+ ListenForExit ( ) ;
183
200
_serverProcess . BeginOutputReadLine ( ) ;
184
201
_serverProcess . BeginErrorReadLine ( ) ;
185
202
await _mediator . Publish ( new ServerStartupEvent ( ) ) ;
@@ -198,6 +215,7 @@ private async Task StopOpenSSH(bool force = false)
198
215
if ( _serverProcess != null )
199
216
{
200
217
_logger . LogDebug ( "Stopping 'sshd' process" ) ;
218
+ _serviceProcessExitAction = null ;
201
219
_serverProcess . Kill ( true ) ;
202
220
_serverProcess . OutputDataReceived -= OnSSHOutput ;
203
221
_serverProcess . ErrorDataReceived -= OnSSHOutput ;
0 commit comments