-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove-StuckProcess.ps1
More file actions
68 lines (56 loc) · 2.17 KB
/
Copy pathRemove-StuckProcess.ps1
File metadata and controls
68 lines (56 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<#
.SYNOPSIS
Beobachtet einen Prozess und beendet ihn, wenn er sich nicht mehr regt.
.DESCRIPTION
Misst in einem festen Intervall die Speichernutzung des Prozesses. Bleibt
sie ueber mehrere Messungen unveraendert, gilt der Prozess als haengend
und wird beendet.
Gedacht fuer Faelle, in denen ein Programm regelmaessig einfriert und
niemand danebensitzt.
.NOTES
Die Heuristik kann danebenliegen: ein Prozess, der korrekt auf Eingaben
wartet, veraendert seine Speichernutzung ebenfalls nicht. Vor dem
unbeaufsichtigten Einsatz mit einem grosszuegigen Intervall beobachten.
#>
function Remove-StuckProcess {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false)]
[string]$processName, # Define the process name and the time interval (in seconds)
[int]$interval = 30 # Change this to the number of seconds you want between checks
)
begin {
}
process {
$processes = Get-Process -Name $processName -ErrorAction SilentlyContinue
if ($processes) {
foreach ($process in $processes) {
# Get the initial RAM usage of the process
$initialMemory = $process.WorkingSet64
# Wait for the specified interval
Start-Sleep -Seconds $interval
# Get the RAM usage of the process again
$finalMemory = (Get-Process -Id $process.Id).WorkingSet64
# Compare the initial and final memory usage
if ($initialMemory -ne $finalMemory) {
Write-Host("Speicher hat sich verändert: {0} {1}" -f $initialMemory, $finalMemory) -ForegroundCOlor Green
Write-Host($initialMemory)
return $true
} else {
Write-Host("Speicher hat sich NICHT verändert: {0} {1}" -f $initialMemory, $finalMemory) -ForegroundCOlor Red
# Kill the process if the memory usage hasn't changed
& taskkill /PID $process.Id /F /T
Write-Host ("Process {0} ({1}) has been killed due to no change in memory usage." -f $processName, $process.Id)
return $false
}
}
} else {
Write-Host ("Process {0} not found " -f $processName) -ForegroundColor Yellow
return $true
}
}
end {
}
}
$processName = "SLDWORKS"
Remove-StuckProcess -processName $processName