@@ -412,6 +412,8 @@ def get_residence_host(self) -> Host:
412412 return self .host .pool .get_host_by_uuid (host_uuid )
413413
414414 def start_background_process (self , cmd : str ) -> str :
415+ if self .is_windows :
416+ logging .warning ('start_background_process is not reliable on Windows' )
415417 script = "/tmp/bg_process.sh"
416418 pidfile = "/tmp/bg_process.pid"
417419 with tempfile .NamedTemporaryFile ('w' ) as f :
@@ -443,8 +445,19 @@ def start_background_process(self, cmd: str) -> str:
443445 self .ssh (f'rm -f { pidfile } ' )
444446 return str (pid )
445447
446- def pid_exists (self , pid : str ) -> bool :
447- return self .ssh_with_result (f'kill -s 0 { pid } ' ).returncode == 0
448+ def pid_exists (self , pid : str , winpid : bool = False ) -> bool :
449+ if self .is_windows and winpid :
450+ return strtobool (
451+ self .execute_powershell_script (f"$null -ne (Get-Process -Id { pid } -ErrorAction SilentlyContinue)" )
452+ )
453+ else :
454+ return self .ssh_with_result (f'kill -s 0 { pid } ' ).returncode == 0
455+
456+ def kill_pid (self , pid : str , winpid : bool = False ) -> None :
457+ if self .is_windows and winpid :
458+ self .execute_powershell_script (f"Stop-Process -Id { pid } -Force -ErrorAction SilentlyContinue" )
459+ else :
460+ self .ssh (f'kill { pid } ' )
448461
449462 @overload
450463 def execute_script (self , script_contents : str , * , simple_output : Literal [True ] = True ) -> str :
@@ -818,17 +831,19 @@ def run_powershell_command(self, program: str, args: str) -> int:
818831 f"Write-Output (Start-Process -Wait -PassThru { program } -ArgumentList '{ args } ').ExitCode" )
819832 return int (output )
820833
821- def start_background_powershell (self , cmd : str ) -> None :
834+ def start_background_powershell (self , cmd : str ) -> str :
822835 """
823- Run command under powershell in the background.
836+ Run command under powershell in the background. Return the PID as string.
824837
825838 Backslash-safe.
826839 """
827840 assert self .is_windows
828841 encoded_command = commands .encode_powershell_command (cmd )
829- self .ssh (
830- "powershell.exe -noprofile -noninteractive Invoke-WmiMethod -Class Win32_Process -Name Create "
842+ return self .ssh (
843+ "powershell.exe -noprofile -noninteractive -command \\ ("
844+ "Invoke-WmiMethod -Class Win32_Process -Name Create "
831845 f"-ArgumentList \\ 'powershell.exe -noprofile -noninteractive -encodedcommand { encoded_command } \\ '"
846+ "\\ ).ProcessId"
832847 )
833848
834849 def is_windows_pv_device_installed (self ) -> bool :
0 commit comments