很容易想到,“你不能在 .bat 中做到这一点”。我知道这是我的第一反应。问题是您需要不直接支持且不重要的日期操作。但随后里奇·劳伦斯( Ritchie Lawrence)来救援,他完成了编写必要的日期函数的所有艰苦工作。
WMIProcess
类CreationDate
以 UTC 格式提供。Win32_OperatingSystem
LocalDateTime
以 UTC 格式为我们提供当前时间。我们需要从 LocalDataTime 中减去最大生命周期(在您的情况下为 30 分钟)以获得 CutOffTime。然后我们可以使用它来过滤Process
,最后call terminate
(而不是taskkill
)。而不是findstr
,我使用 WMIwhere
过滤器(这要快得多)。
以下代码似乎有效。由于这是KILLING TASKS,您应该自己测试一下。
注意:if "%%p" GEQ "0"
用于过滤掉 wmic 结果末尾的“空白”行,该行不为空,但包含换行符。正如我们所期待的那样,这似乎是一个简单而有效的测试(尽管也许有更好的方法来处理这个问题)。
@echo off
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
set MaxRunningMinutes=30
set ProcessName=firefox.exe
for /f "usebackq skip=1" %%t in (
`wmic.exe path Win32_OperatingSystem get LocalDateTime`) do (
if "%%t" GEQ "0" set T=%%t)
rem echo !T!
rem echo !T:~,4!/!T:~4,2!/!T:~6,2! !T:~8,2!:!T:~10,2!:!T:~12,2!
rem echo !T:~15,-4! !T:~-4!
set fsec=!T:~15,-4!
set tzone=!T:~-4!
call :DateToSecs !T:~,4! !T:~4,2! !T:~6,2! !T:~8,2! !T:~10,2! !T:~12,2! UNIX_TIME
rem echo !UNIX_TIME!
set /a CutOffTime=UNIX_TIME-MaxRunningMinutes*60
rem echo !CutOffTime!
call :SecsToDate !CutOffTime! yy mm dd hh nn ss
rem echo !yy!/!mm!/!dd! !hh!:!nn!:!ss!
set UTC=!yy!!mm!!dd!!hh!!nn!!ss!.!fsec!!tzone!
rem echo !UTC!
wmic process where "name='%ProcessName%' AND CreationDate<'%UTC%'" call terminate
rem * Alternate kill method. May be useful if /F flag is needed to
rem * to forcefully terminate the process. (Add the /F flag to
rem * taskill cmd if needed.)
rem for /f "usebackq skip=1" %%p in (
rem `wmic process where "name='%ProcessName%' AND CreationDate<'%UTC%'" get processid`) do (
rem if "%%p" GEQ "0" taskkill /PID %%p)
goto :EOF
rem From: http://www.commandline.co.uk/lib/treeview/index.php
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
::
:: By: Ritchie Lawrence, updated 2002-08-13. Version 1.1
::
:: Func: Returns number of seconds elapsed since 1st January 1970 00:00:00
:: for a given calendar date and time of day. For NT4/2000/XP/2003.
::
:: Args: %1 year to convert, 2 or 4 digit (by val)
:: %2 month to convert, 1/01 to 12, leading zero ok (by val)
:: %3 day of month to convert, 1/01 to 31, leading zero ok (by val)
:: %4 hours to convert, 1/01 to 12 for 12hr times (minutes must be
:: suffixed by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
:: %5 mins to convert, 00-59 only, suffixed by a/p if 12hr (by val)
:: %6 secs to convert, 0-59 or 00-59 (by val)
:: %7 var to receive number of elapsed seconds (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set ss=%6
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a j=j*86400+hh*3600+nn*60+ss
endlocal&set %7=%j%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:SecsToDate %secs% yy mm dd hh nn ss
::
:: By: Ritchie Lawrence, updated 2002-07-24. Version 1.1
::
:: Func: Returns a calendar date and time of day from the number of
:: elapsed seconds since 1st January 1970 00:00:00. For
:: NT4/2000/XP/2003.
::
:: Args: %1 seconds used to create calendar date and time of day (by val)
:: %2 var to receive year, 4 digits for all typical dates (by ref)
:: %3 var to receive month, 2 digits, 01 to 12 (by ref)
:: %4 var to receive day of month, 2 digits, 01 to 31 (by ref)
:: %5 var to receive hours, 2 digits, 00 to 23 (by ref)
:: %6 var to receive minutes, 2 digits, 00 to 59 (by ref)
:: %7 var to receive seconds, 2 digits, 00 to 59 (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set /a i=%1,ss=i%%60,i/=60,nn=i%%60,i/=60,hh=i%%24,dd=i/24,i/=24
set /a a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
(if %hh% LSS 10 set hh=0%hh%)&(if %nn% LSS 10 set nn=0%nn%)
if %ss% LSS 10 set ss=0%ss%
endlocal&set %7=%ss%&set %6=%nn%&set %5=%hh%&^
set %4=%dd%&set %3=%mm%&set %2=%yy%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
使用 Powershell
最初的问题被标记为“批处理文件”,所以我首先给出了 Windows bat 答案。但我想提一下,杀死一个运行时间超过 30 分钟的命名进程对于 Powershell 来说是微不足道的:
Get-Process firefox |
Where StartTime -lt (Get-Date).AddMinutes(-30) |
Stop-Process -Force
再次,这是杀死任务,所以请小心。如果您不熟悉 Powershell,这里是一个内衬的细分:
- 使用
Get-Process
cmdlet 获取在本地计算机上运行的进程。这里传递了进程的名称firefox
。如果需要,可以传递名称列表,并且可以使用通配符。
- 由返回的进程对象通过
Get-Process
管道传送到Where
StartTime 属性上的过滤器。cmdlet 用于获取当前Get-Date
日期和时间作为DateTime
类型。调用 AddMinutes 方法添加 -30 分钟,返回一个DateTime
表示 30 分钟前的值。运算符(从-lt
技术上讲,在此上下文中,它是一个开关参数),指定小于运算。
- 由返回的过滤后的进程对象通过
Where
管道传送到Stop-Process
cmdlet。该-Force
参数用于防止确认提示。
上面我使用了Where
Powershell 3 中引入的简化语法。如果您需要 Powershell 2 兼容性(这是在 Windows XP 上运行的最新版本),则需要此语法:
Get-Process firefox |
Where { $_.StartTime -lt (Get-Date).AddMinutes(-30) } |
Stop-Process -Force
这里的花括号包含一个 ScriptBlock。$_
用于显式引用当前对象。