You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cleanup.ps1 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. #
  20. # Powershell script for cleaning up remaining browser and selenium server processes on the CI servers
  21. #
  22. # Author: Martin Stockhammer <martin_s@apache.org>
  23. # Date : 2017-04-30
  24. #
  25. # Descriptions:
  26. # Stopps processes related to the selenium checks, if they were not stopped by the selenium server, because
  27. # the job was aborted.
  28. # The script cannot determine, which of the processes are started by the current job, so if there are
  29. # parallel jobs running on this server that start processes with the same name and user, these
  30. # will be stopped too.
  31. #
  32. # Per default the script will stop "firefox.exe","iexplore.exe","chrome.exe"
  33. # and the processes "java.exe","mshta.exe" if their commandline arguments contain "selenium-server"
  34. #
  35. # Parameters:
  36. # -Verbose : If set, more output will be printed
  37. # -Browsers proc1,proc2 : The list of executables that define the browser processes, that are started by selenium
  38. # -SeleniumProcesses : The list of processes with the string "selenium-server" in the commandline arguments
  39. param (
  40. [switch]$Verbose = $False,
  41. [String[]]$Browsers = @("firefox.exe","iexplore.exe","chrome.exe"),
  42. [String[]]$SeleniumProcesses = @("mshta.exe","java.exe","IEDriverServer.exe")
  43. )
  44. # $currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
  45. $currentUser = $env:UserName
  46. Write-Output "User: $currentUser"
  47. if ($Verbose)
  48. {
  49. Get-Process | Get-Member
  50. $View = @(
  51. @{l='Handles';e={$_.HandleCount}},
  52. @{l='NPM(K)';e={ (Get-Process -Id $_.ProcessId).NonpagedSystemMemorySize/1KB -as [int]}},
  53. @{l='PM(K)';e={ $_.PrivatePageCount/1KB -as [int]}},
  54. @{l='WS(K)';e={ $_.WorkingSetSize/1KB -as [int]}},
  55. @{l='VM(M)';e={ $_.VirtualSize/1mB -as [int]}},
  56. @{l='CPU(s)';e={ (Get-Process -Id $_.ProcessId).CPU -as [int]}},
  57. @{l='Id';e={ $_.ProcessId}},
  58. 'UserName'
  59. @{l='ProcessName';e={ $_.ProcessName}}
  60. )
  61. Get-WmiObject Win32_Process | % { $_ |
  62. Add-Member -MemberType ScriptProperty -Name UserName -Value {
  63. '{0}\{1}' -f $this.GetOwner().Domain,$this.GetOwner().User
  64. } -Force -PassThru
  65. }
  66. }
  67. foreach ($procName in $SeleniumProcesses)
  68. {
  69. $processes = Get-WmiObject Win32_Process -Filter "name = '$procName'" | Where-Object {$_.GetOwner().User -eq $currentUser } | Where-Object {$_.CommandLine -match "selenium-server"}
  70. if ($Verbose) {
  71. Write-Output "Filter: name = '$procName'"
  72. }
  73. foreach($proc in $processes)
  74. {
  75. Write-Output "stopping proccess $($proc.ProcessId) with $($proc.ThreadCount) threads; $($proc.CommandLine.Substring(0, 50))..."
  76. Stop-Process -F $proc.ProcessId
  77. }
  78. }
  79. foreach ($procName in $Browsers)
  80. {
  81. $processes = Get-WmiObject Win32_Process -Filter "name = '$procName'" | Where-Object {$_.GetOwner().User -eq $currentUser }
  82. if ($Verbose) {
  83. Write-Output "Filter: name = '$procName'"
  84. }
  85. foreach($proc in $processes)
  86. {
  87. Write-Output "stopping proccess $($proc.ProcessId) with $($proc.ThreadCount) threads; $($proc.CommandLine.Substring(0, 50))..."
  88. Stop-Process -F $proc.ProcessId
  89. }
  90. }