admin管理员组

文章数量:1277313

I am writing using JavaScript. I have a PID of a process. How do I kill it? You can terminate by a name using WMI, How can you do it using PID?

UPDATE: The platform is Windows.

I am writing using JavaScript. I have a PID of a process. How do I kill it? You can terminate by a name using WMI, How can you do it using PID?

UPDATE: The platform is Windows.

Share Improve this question edited Feb 7, 2010 at 12:26 Andy E 345k86 gold badges481 silver badges451 bronze badges asked Feb 6, 2010 at 19:23 Boris RaznikovBoris Raznikov 2,45310 gold badges35 silver badges57 bronze badges 6
  • 1 kill -9 PID ??? you must be talking about a Rhino VM I guess. – jldupont Commented Feb 6, 2010 at 19:24
  • 1 i doubt any system is going to let Javascript do root level mands! – PurplePilot Commented Feb 6, 2010 at 19:27
  • You need to tell us in what environment this is running. Is it JScript on the Windows Script Host, for example? – Anonymous Commented Feb 6, 2010 at 19:35
  • 3 @PurplePilot: Windows shell scripts can be written in Javascript (JScript). Who voted that this belongs on superuser? it's clearly a programming question. – Andy E Commented Feb 6, 2010 at 19:35
  • 2 @Andy E Thanks didn't know. I think i should have qualified it being from/in a browser, which should be sandboxed. If you really tried you could run anything from anywhere ;-) – PurplePilot Commented Feb 6, 2010 at 19:55
 |  Show 1 more ment

2 Answers 2

Reset to default 9

It looks like you're coding for either Windows Script Host or a Windows Desktop Gadget. If it is, I would use WScript.Shell and its Exec method along with the mand line taskkill (Win XP Pro, Win Vista & Win 7 only):

var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("taskkill /pid 1234");

If you really want to do it with WMI something like the following works fine for me (thanks @Helen for the improvements):

function killPID (pid) {
  GetObject("winmgmts:").Get("Win32_Process.Handle='" + pid + "'").Terminate();
}

For Windows 2000 you will need to install the Windows Support Tools and then use the Kill mand from the shell as Andy E described in his answer..

Reference: https://web.archive/web/1/http://articles.techrepublic%2e%2e/5100-10878_11-5031568.html

本文标签: wshJavaScript killing a processStack Overflow