Process - Wait for or Force close

Octopus.Script exported 2015-11-21 by svenkle belongs to ‘Windows’ category.

Waits a set amount of time for a process to close and optionally force closes the process after the timeout expires.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Seconds

Seconds = 10

The number of seconds to wait before forcefully killing the application

Force Kill

Force = True

Whether or not the application should be forcefully killed after the timeout has expired.

Process Name

ProcessName

The name of the process to wait for

Script body

Steps based on this template will execute the following PowerShell script.

$seconds = $OctopusParameters['Seconds']
$forceCloseOnTimeout = $OctopusParameters['Force']
$processName = $OctopusParameters['ProcessName']
$timeout = new-timespan -Seconds $seconds
$stopwatch = [diagnostics.stopwatch]::StartNew()

# Check if the process is even running
if (Get-Process $processName -ErrorAction silentlycontinue)
{
    Write-Host "Waiting $seconds seconds for process '$processName' to terminate"
} 
else 
{
    Write-Host "Process '$processName' is not running"
    return
}

# Count down waiting for the process to stop gracefully
while ($stopwatch.elapsed -lt $timeout)
{
    # Check process is running
    if (Get-Process $processName -ErrorAction silentlycontinue) 
    {
        Write-Host "Waiting..."
    }
    else 
    {
        Write-Host "Process '$processName' is no longer running"
        return
    }

    # Wait for a while
    Start-Sleep -seconds 1
}

# Force close the process if set
if($forceCloseOnTimeout –eq $TRUE)
{
    Write-Host "Force closing process $processName"
    Stop-Process -processname $processName -Force
    Write-Host "Process '$processName' is no longer running"
    return
}

Write-Host "Process $processName didn't close within the allocated time"
Write-Host "Continuing anyway"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "2d59d5b6-1dc3-424e-9279-676393933efd",
  "Name": "Process - Wait for or Force close",
  "Description": "Waits a set amount of time for a process to close and optionally force closes the process after the timeout expires.",
  "Version": 0,
  "ExportedAt": "2015-11-21T01:21:41.951+00:00",
  "ActionType": "Octopus.Script",
  "Author": "svenkle",
  "Parameters": [
    {
      "Name": "Seconds",
      "Label": "Seconds",
      "HelpText": "The number of seconds to wait before forcefully killing the application",
      "DefaultValue": "10",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Force",
      "Label": "Force Kill",
      "HelpText": "Whether or not the application should be forcefully killed after the timeout has expired.",
      "DefaultValue": "True",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    },
    {
      "Name": "ProcessName",
      "Label": "Process Name",
      "HelpText": "The name of the process to wait for",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "$seconds = $OctopusParameters['Seconds']\n$forceCloseOnTimeout = $OctopusParameters['Force']\n$processName = $OctopusParameters['ProcessName']\n$timeout = new-timespan -Seconds $seconds\n$stopwatch = [diagnostics.stopwatch]::StartNew()\n\n# Check if the process is even running\nif (Get-Process $processName -ErrorAction silentlycontinue)\n{\n    Write-Host \"Waiting $seconds seconds for process '$processName' to terminate\"\n} \nelse \n{\n    Write-Host \"Process '$processName' is not running\"\n    return\n}\n\n# Count down waiting for the process to stop gracefully\nwhile ($stopwatch.elapsed -lt $timeout)\n{\n    # Check process is running\n    if (Get-Process $processName -ErrorAction silentlycontinue) \n    {\n        Write-Host \"Waiting...\"\n    }\n    else \n    {\n        Write-Host \"Process '$processName' is no longer running\"\n        return\n    }\n\n    # Wait for a while\n    Start-Sleep -seconds 1\n}\n\n# Force close the process if set\nif($forceCloseOnTimeout –eq $TRUE)\n{\n    Write-Host \"Force closing process $processName\"\n    Stop-Process -processname $processName -Force\n    Write-Host \"Process '$processName' is no longer running\"\n    return\n}\n\nWrite-Host \"Process $processName didn't close within the allocated time\"\nWrite-Host \"Continuing anyway\""
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-wait-for-or-force-close-application.json",
  "Website": "/step-templates/2d59d5b6-1dc3-424e-9279-676393933efd",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////Da3qSsLvhtb0wur6O7zuWcfxldv2aMzyK7ftpOD3s+X48Pr+0fD7d9HzHLLr4fX8xD/OcwAAAaNJREFUeNrs3cFygjAUQFECWott1f//2sJoW6kIKEzNs+euXOmcmSSGDa8oJEmSJEmSJGmsj1W1K9cpsGD1Vr2WdToVEPC+2lYvZfpVrEW0qZpF1F+MRdRugzoNlvkiarfBPk0pT8GhWUSX2yASpDlLr2+DEJBmEY1ug6whx7N0n2b30G1QlmmxHsRYp6X76yvF9vg5RYQczq8UVURI35UiFmTgShED0p6lI1eKzCHTrxS5Qk6PZ9PLDtJ9PIsJmXWlyAky6/dAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQMJCyjltF/iO3gpJUpD8s4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgID8T8itwwKyhbTdMr4ha8hXUwZqhICcOgyNOIkE+V5wo4MSgr1u/fp7poO+AL8K/gL8yw0UeyRB34m9iQ/pVD8L5JYTO3NI58R+AsiEEzsW5OfE3sUe/zRwYkeGnG2g2CPS7rhjF4GKP0ZwyoldxK37kFqEL/7wU0mSJEmSJOmJ+xRgAHxZTCXGdZkfAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Saturday, November 21, 2015