Recipe 10.5. Killing a Process


Problem

You want to terminate a process. Even though Windows has come a long way in the last 10 years, the operating system can't prevent buggy or poorly written applications from becoming unresponsive, which means you may need to manually terminate processes from time to time.

Solution

Using a graphical user interface

  1. Open the Windows Task Manager (taskmgr.exe).

  2. Click on the Processes tab.

  3. If you do not see the process you want to set, be sure the box beside Show processes from all users is checked.

  4. Right click on the target process, select End Process, and select the desired priority.

  5. You can also accomplish the same task using the Sysinternals Process Explorer (procexp.exe) tool. Right-click the process and select Kill Process.

Using a command-line interface

This command kills a process by PID:

> taskkill -pid <PID>

And this kills a process by name:

> taskkill  -im <ProcessName>

Use the /f option to forcefully kill the process.

Using downloadable software

The pskill.exe utility works in a very similar manner to taskkill. You can specify the PID to kill:

> pskill <PID>

Or the process name:

> pskill <ProcessName>

Using VBScript
' This code terminates the specified process. ' ------ SCRIPT CONFIGURATION ------ intPID = 2560   ' PID of the process to terminate strComputer = "." ' ------ END CONFIGURATION --------- WScript.Echo "Process PID: " & intPID set objWMIProcess = GetObject("winmgmts:\\" & strComputer & _                     "\root\cimv2:Win32_Process.Handle='" & intPID & "'") WScript.Echo "Process name: " & objWMIProcess.Name intRC = objWMIProcess.Terminate( )  if intRC = 0 Then    Wscript.Echo "Successfully killed process." else    Wscript.Echo "Could not kill process. Error code: " & intRC end if

Discussion

Manually killing processes is not good practice, but it is a necessary evil. Be selective about forcibly killing a process, because this will also terminate any child processes in an ungraceful manner and can leave lingering remnants of the process in memory, which may cause problems if you attempt to restart the process later.



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net