PowerShell / Win32-OpenSSH

Win32 port of OpenSSH
7.22k stars 742 forks source link

Allow Breakaway OK on SSH Job #1032

Closed jborean93 closed 6 years ago

jborean93 commented 6 years ago

"OpenSSH for Windows" version 1.0.0.0

Server OperatingSystem Windows Server 2016 Standard Evaluation Still applies to all other hosts as well

Client OperatingSystem N/A (tested on MacOS though)

What is failing The ssh-shellhost.exe process that is spawned on an SSH connection does not allow processes to breakaway from the job. This means any process it spawns won't be able to breakaway from the SSH job. This is useful if you want to run a process while not keeping the SSH connection open.

As you can see by Process Explorer, the only limit placed on the Job is Kill on Job Close

image

Compared to a process that is spawned by WinRM, it contains some more job limits that are mostly configurable by the service. The key one I am looking for is Breakaway OK which allows the process to call the CreateProcess (and derivative) the CREATE_BREAKAWAY_FROM_JOB creation flag https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx. You can see this based on the cmd.exe process that I spawned on a WinRM process

image

I'm not sure if this is intentional on your part but it would be good to hear your thoughts on this as this is a key feature Ansible uses to achieve async support on the current WinRM connection. If we were to loose this feature, our options are limited and we may need to resort to scheduled tasks which aren't really that easy to manage.

On a side note this is not a "regression" of the 1.0.0.0 release as this has been the case for the past few releases.

manojampalam commented 6 years ago

This is intentional to keep the implementation in line with how it works on Unix. Otherwise, you may use Task Scheduler to achieve background job/process execution.

jborean93 commented 6 years ago

@manojampalam thanks for the update, we will have to see what our other options are. In regards to keeping things how they work on Unix, I'm not 100% confident as to how it all works but you can easily run a process "outside" of the SSH shell. For example I can run

nohup cmd="echo"; for i in {1..50}; do $cmd "count: $i"; sleep 1;done > stdout.txt &

Once I exit from the SSH session, the process is still running in the background and the SSH tty no longer exists when running who. I would have thought this would be similar to Windows when running CreateProcess with the CREATE_BREAKAWAY_FROM_JOB creation flag on the process.

IMO task scheduler is a massive pain when it comes to things like this as;

Ultimately if that is what we need to do then that's what we need to do but I'm hoping to avoid it if possible :)

nitzmahone commented 6 years ago

@manojampalam Agree 100% with @jborean93 - the job handling is a fine way to deal with cleaning up after a session closes, but processes that explicitly request breakaway should be allowed to do so (ala nohup on POSIX systems).

manojampalam commented 6 years ago

Thanks for following up and apologies for a misunderstanding of the original ask. Yes, we should allow child processes to break away at their liberty. This will be fixed in the next drop.

Attached a private if you want to try out. ssh-shellhost.zip

jborean93 commented 6 years ago

Fantastic, I just tried it out locally and it works as you say, it even fixed another issue where we were creating some jobs, looking forward to the release.

RDCH106 commented 6 years ago

Hi!

I am using Win32-OpenSSH and have read this issue searching a solution to the process killing when the ssh connection is closed.

According to the release notes version v7.6.0.0p1-Beta let applications break away from ssh session. I am using version v7.6.1.0p1-Beta 64 bits version but I can not launch processes in background and they don't continue working when the ssh session is closed.

What is the way to achieve this?

Thanks in advance!

jborean93 commented 6 years ago

@RDCH106, we currently achieve this through some P/Invoke calls in PoweShell and call the CreateProcess Win32 API with a specific flag. This isn't something simple to do and requires a bit of code to achieve but the easiest way I know of to do this in PowerShell is to use WMI. Here is an example script that uses Win32_Process as sets the CREATE_BREAKAWAY_FROM_JOB flag when creating the process

$proc_info = New-CimInstance -CimClass (Get-CimClass -ClassName Win32_ProcessStartup) -Property @{CreateFlags=16777216} -ClientOnly
$arguments = @{CommandLine="cmd.exe"; ProcessStartupInformation=$proc_info}
Invoke-CimMethod -ClassName Win32_Process -Name Create -Arguments $arguments
RDCH106 commented 6 years ago

@jborean93 Thanks for your answer!

DerekBennett commented 6 years ago

I am using OpenSSH-Win64-v7.7.0.0p1-Beta to invoke a BAT file remotely. The BAT file sets some environment variables and then calls START "" someprogram.exe. This used to work just fine in prior releases of SSH. Now, however, START no longer works. Instead the someprogram.exe instance dies as soon as the BAT file exits. Is this a bug, or should I be doing something differently?

manojampalam commented 6 years ago

@DerekBennett can you please open a new issue and add a reference to this thread?

DerekBennett commented 6 years ago

done

On Thu, Jun 21, 2018 at 12:01 PM Manoj Ampalam notifications@github.com wrote:

@DerekBennett https://github.com/DerekBennett can you please open a new issue and add a reference to this thread?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/PowerShell/Win32-OpenSSH/issues/1032#issuecomment-399155760, or mute the thread https://github.com/notifications/unsubscribe-auth/ABz_P9sEOjKl_UPLf2Jm7KIKnFsJ-Xvoks5t-8NugaJpZM4RpQiq .

-- Derek Bennett Derek@TheBennetts.org 1.917.968.8982