如何在每隔30分钟后继续检查流程? [英] How to keep checking the process after every 30 minutes?
本文介绍了如何在每隔30分钟后继续检查流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
kill the process
如果开始时间是less than 2 hours.
如果开始时间是more than 2 hours.
,我需要add sleep for 30 mins
我需要keep repeating
它,直到进程不再运行。
到目前为止,我已经编写了以下脚本来执行上述操作。
$procName = 'myprocess'
$process = Get-Process | Where-Object Name -EQ $procName
if(-not $process) {
Write-Warning "$procName not found!"
}
else {
$process | ForEach-Object {
if($_.StartTime -lt [datetime]::Now.AddHours(-2)) {
Stop-Process $_ -Force
}
else {
sleep(1800)
}
}
}
}
如何将上述程序添加到do-while
或另一个循环中,以保持检查,直到进程不再运行?
另外,如何实现4小时的最大定时器?
推荐答案
如果我理解正确的话,您的else
条件可能使用do-while
循环:
else {
do {
"$procName is still running, sleeping for 1800 sec"
Start-Sleep -Seconds 1800
} while(Get-Process | Where-Object Name -EQ $procName)
}
但是,请注意,如果进程从未停止或您实现了最大计时器等,这可能会导致无限循环。
根据您关于实现最大计时器的评论,有许多方法可以做到这一点,我个人的偏好是使用StopWatch
:
else {
$timer = [System.Diagnostics.Stopwatch]::StartNew()
do {
# Do this while the process is still running AND
# the timer hasn't yet reached 4 hours
"$procName is still running, sleeping for 1800 sec"
Start-Sleep -Seconds 1800
$stillRunning = Get-Process | Where-Object Name -EQ $procName
} while($stillRunning -and $timer.Elapsed.Hours -lt 4)
$timer.Stop()
}
这篇关于如何在每隔30分钟后继续检查流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文