Recipe 6.3. Killing a ProcessProblemYou want to terminate a process. Even though Windows has come a long way in the past 10 years, the operating system can't prevent buggy or poorly written applications from becoming unresponsive. SolutionUsing a graphical user interface
Using a command-line interfaceThe following command kills a process by PID: > taskkill -pid <PID> And this command kills a process by name on a remote server: > taskkill /s <ServerName> -im <ProcessName> Use the /f option to forcefully kill the process. The pskill.exe utility works in a very similar manner. Here are two examples: > pskill <PID> > pskill \\<ServerName> <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 DiscussionManually killing processes is not something you should be in the habit of doing, but it is a necessary evil of system administration. Be selective about forcibly killing a process, because it 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.
|