# $cs.Sys_WaitForProcess

> Waits for an already-running process to exit, checking every IntervalSec seconds up to a total of MaxWaitSec seconds.

Source: https://docs.capaone.com/capainstaller/powerpacks/cs-sys-waitforprocess/  
Product: CapaInstaller — a separate CapaSystems product; do not apply this page to any other.

### Description

Waits for an already-running process to exit, checking every `IntervalSec` seconds up to a total of `MaxWaitSec` seconds. If the process isn't running to begin with, returns immediately with no error. Important: this function always returns silently, whether the process actually exited or the timeout was simply reached — there is no return value to tell you which happened. If you need to know, check `$cs.Sys_ExistProcess(...)` again afterward yourself.

### Syntax

$cs.Sys_WaitForProcess(string processname, int MaxWaitSec, int IntervalSec)

### Parameters

#### processname (String)

Name of the process to wait for.

#### MaxWaitSec (Integer)

Maximum total number of seconds to wait for the process to exit.

#### IntervalSec (Integer)

Number of seconds to sleep between each check.

### Return value

None.

### Example

**PowerShell**

Wait for a setup process to finish, then check `$cs.Sys_ExistProcess` afterward to tell whether it actually exited or the wait simply timed out:

```powershell
if ($cs.Sys_ExistProcess("setup.exe"))
{
  $cs.Job_WriteLog("Waiting up to 60 seconds for setup.exe to finish")
  $cs.Sys_WaitForProcess("setup.exe", 60, 1)
  if ($cs.Sys_ExistProcess("setup.exe"))
  {
    $cs.Job_WriteLog("setup.exe is still running after timeout")
  }
}
```

Wait for a slow installer's background helper process to finish before cleaning up temporary files:

```powershell
$cs.Job_WriteLog("Waiting up to 300 seconds for the installer helper process to complete")
$cs.Sys_WaitForProcess("InstallerHelper", 300, 5)
if ($cs.Sys_ExistProcess("InstallerHelper"))
{
  $cs.Job_WriteLog("InstallerHelper did not finish in time, forcing it closed before cleanup")
  $cs.Sys_KillProcess("InstallerHelper")
}
Remove-Item -Path "$env:TEMP\InstallerHelper" -Recurse -Force -ErrorAction SilentlyContinue
```

### Related functions

[$cs.Sys_ExistProcess](/capainstaller/powerpacks/cs-sys-existprocess/) [$cs.Sys_WaitForProcessToExist](/capainstaller/powerpacks/cs-sys-waitforprocesstoexist/) [$cs.Sys_KillProcess](/capainstaller/powerpacks/cs-sys-killprocess/)
