如果你安装了 Wscript - 我认为应该是 - 你可以试试这个。另存为“somethingorother.vbs”并不时执行。或者您可以使用提供的循环。loop 和 kill-for-real 选项都有注释;在生产服务器上取消注释之前测试脚本。
我假设 'mysqladmin processlist' 输出类似这样的内容(取自我的 XP):
+----+------+----------------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+----------------+----+---------+------+-------+------------------+
| 21 | root | localhost:1648 | | Query | 0 | | show processlist |
+----+------+----------------+----+---------+------+-------+------------------+
所以我们要去掉包含“----”的行和包含“Id | User”的行,剩下的就是
| 21 | root | localhost:1648 | | Query | 0 | | show processlist |
我们可以用管道符号“|”分割,得到从 0 到 8 的九个字段:
0 1 2 3 4 5 6 7 8
| 21 | root | localhost:1648 | | Query | 0 | | show processlist |
字段 #1 是 Id,字段 #6 产生运行时间。
Option Explicit
Dim objShell, objWshScriptExec, objStdOut, strLine
Dim id, fields, rt, Killer, KillRun
' While True
' WScript.Sleep 150000
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec("mysqladmin processlist")
Set objStdOut = objWshScriptExec.StdOut
While Not objStdOut.AtEndOfStream
strLine = objStdOut.ReadLine
If (InStr(strLine, "----") = 0 and InStr(strLine, "| Id | User |") = 0) Then
fields = Split(strLine, "|")
id = trim(fields(1))
rt = trim(fields(6))
If rt > 150 Then
' Set Killer = CreateObject("WScript.Shell")
' Set KillRun = objShell.Exec("mysqladmin kill " & id)
echo
End If
End If
Wend
' Wend