Last active
December 14, 2022 12:16
-
-
Save wesleyel/6afb6f34c60303918cfb4e7e5af89796 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$process_name = "notepad.exe" | |
$time_minute_limit = 0.1 | |
while ($true) { | |
$process = Get-Process | Where-Object { $_.Path -like "*$process_name" } | |
if ($process) { | |
$last_time = (New-TimeSpan -Start ($process).StartTime).TotalMinutes | |
Write-Output "$process_name is running, live time is $([math]::round($last_time,3)) minutes" | |
if ($last_time -gt $time_minute_limit) { | |
$process | Stop-Process -Force | |
Write-Output "Now is $(Get-Date) , tried to kill $($process.ProcessName) , last_time is $([math]::round($last_time,5)) minutes" | |
Write-Output "Process is alive: $(!$process.HasExited)" | |
} | |
Start-Sleep 5 | |
} | |
Start-Sleep 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment