每个人都知道如何使用Windows 中的任务管理器(Task Manager)或OS X中的(OS X)强制退出(Windows or Force Quit)来终止程序,但有时使用命令行(command line)终止程序很有用。我遇到过几种情况,程序拒绝通过Task Manager结束,即使我试图杀死底层进程也是如此。Force Quit有它自己的怪癖,并不总是像它应该的那样杀死一个程序。那时您可以使用命令行(command line)的强大功能。
在本文中,我将介绍在Windows、OS X 和 Linux(OS X and Linux)中杀死程序的命令。有趣的是,通常有不止一个命令可以完成此操作,因此我将尝试提及我使用过的不同命令。这不是一个详尽的列表,因此如果您使用此处未提及的其他命令,请在评论中告知我们。
Windows – TSKILL 和 TASKKILL
在Windows中,您可以使用两个命令来终止程序:TSKILL 和 TASKKILL(TSKILL and TASKKILL)。TSKILL是一个更简单且功能较弱的命令,但可以很好地完成工作。例如,如果您正在运行Microsoft Word,则进程名称(process name)为 winword.exe。要从命令行(command line)中杀死Word,只需键入以下命令:
tskill winword
这将杀死Word,您将丢失任何未保存的数据,因此您必须小心使用它。我在一个未保存的Word 文档(Word doc)上尝试了它,当我运行它时它就消失了,没有提示保存文档。这对于我要在这里提到的所有命令来说都是如此,因为这就是重点。您可以毫无疑问地立即终止程序。
需要注意的一点是,在使用此命令的大多数情况下,它与 /A 参数一起使用。/A 告诉命令结束在所有会话下运行的进程。因此,通常您会键入以下命令以确保进程被终止:
tskill /A winword
第二个命令是TASKKILL(TASKKILL),它有更多的选项和更强大的功能。如果您查看 TASKKILL 的帮助页面(TASKKILL),(help page)您会明白我的意思:
如果您想在Windows中对终止程序有更多控制和选项(control and options),请使用TASKKILL。对于初学者,您可以使用以下命令终止程序:
taskkill /F /IM winword.exe
请注意,使用TASKKILL 命令(TASKKILL command)时必须使用 .EXE 。/F 表示强制终止进程。/IM 表示映像名称(image name),即进程名称(process name)。如果要使用进程 ID ( PID ) 终止,则必须使用 / PID而不是 /IM。/T 很棒,因为它会杀死指定进程启动的所有子进程。
您还可以使用TASKKILL远程连接到另一个系统并终止该远程系统上的进程。我还喜欢在TASKKILL中使用通配符的能力。
OS X/Linux – KILL 和 KILLALL
在OS X 和 Linux(OS X and Linux)中,您有两个用于杀死进程的命令:KILL 和 KILLALL(KILL and KILLALL)。您必须在终端窗口(terminal window)中运行这些。为了杀死一个程序,您必须使用程序名称(program name)或进程 ID。有一些方法可以找到这些信息。一种方法是通过Activity Monitor。
但是,这需要GUI 界面(GUI interface)。如果您使用命令行(command line)来终止进程,您也可以使用命令行(command line)来查找进程信息(process info)。想到的两个命令是top和ps -ax。
top将为您提供一个进程列表,其中包含PID和程序名称(program name),也按CPU 使用率(CPU usage)排序。这是找到要杀死的进程的快速方法。ps -ax将为您提供按PID和程序路径排序的列表。它与顶部略有不同。
现在用于杀死OS X中的程序。您可以简单地键入以下命令来终止特定进程:
kill -9 83002
83002是终端进程(Terminal process),9表示终止进程。您可以使用其他数字,例如 3 表示Quit或 6 表示Abort。不过,大多数情况下(Mostly),您将坚持使用 9。您也可以使用KILL 命令(KILL command)来终止所有正在运行的进程,尽管您可能永远不应该使用此命令。
kill -TERM -1
KILL用于杀死一个进程或所有进程,KILLALL用于(KILLALL)杀死一组进程。例如,如果您正在运行Google Chrome,则可能有 10 个Chrome进程正在运行。十次使用KILL(KILL)来关闭Chrome真的很烦人。相反,您可以像这样使用KILLALL:
killall Evernote
or
killall 'Google Chrome'
请注意,您必须使用单引号或任何超过一个单词的内容。此外,如果该进程不是以您的名义运行,而是在 root 下运行,您必须使用以下命令:
sudo killall 'Google Chrome'
要么
sudo killall -9 'Google Chrome'
同样, 9 正在向KILL(KILL)而不是TERM发送特定信号。仅当您收到有关没有权限的错误时才需要Sudo 。否则,您可以执行killall program或killall -9 program。在OS X上,当您无法强制退出程序时,KILLALL 命令非常方便。(KILLALL command)您不必知道进程 ID,这很好。只需输入(Just type)名称,所有与该名称相关的进程都将被终止。
本文旨在为您提供在Windows、OS X 和 Linux(OS X and Linux)中杀死程序的更高级方法。如果您对使用命令行(command line)终止程序有任何疑问,请发表评论,我会尽力提供帮助。享受!
How to Use the Command Line to Kill a Program
Everyone knows how to kіll a program using Task Manager in Windows or Force Qυit in OЅ X, but sometimes it’s useful to kill a program using the command line. I’ve run into several situations whеre the program refused to end via Task Manager, even when I tried to kill the underlying process. Force Quit has іt’s own quirkѕ аnd doesn’t always kill a program lіke it should. That’s when you can use the power of the cоmmand line.
In this article, I’ll go through the commands for killing a program in Windows, OS X and Linux. Interestingly, there is usually more than one command for accomplishing this, so I’ll try to mention the different ones that I have used. It’s not an exhaustive list, so if you use a different command not mentioned here, please let us know in the comments.
Windows – TSKILL and TASKKILL
In Windows, you can use two commands to kill a program: TSKILL and TASKKILL. TSKILL is a simpler and less powerful command, but does the job just fine. For example, if you’re running Microsoft Word, the process name is winword.exe. To kill Word from the command line just type the following command:
tskill winword
That will kill Word and you will lose any unsaved data, so you have to be careful using it. I tried it out on an unsaved Word doc and it just disappeared when I ran this, no prompts to save the documents. This is pretty much true of all the commands I’m going to mention here as that’s kind of the point. You can to kill a program instantly without any questions.
One thing to note is that most of the time that this command is used, it’s used with the /A parameter. /A tells the command to end the process running under all sessions. So typically you would type the following command to be extra sure the process is killed:
tskill /A winword
The second command, which has more options and is more powerful, is TASKKILL. If you look at the help page for TASKKILL, you see what I mean:
If you want to have more control and options for killing a program in Windows, use TASKKILL. For starters, you can kill a program by using the following command:
taskkill /F /IM winword.exe
Note that you have to use .EXE when using the TASKKILL command. /F means to forcefully terminate the process forcefully. /IM means the image name, i.e. the process name. If you want to kill using the process ID (PID), you have to use /PID instead of /IM. /T is great because it will kill all child processes started by the specified process.
You can also use TASKKILL to remotely connect to another system and kill a process on that remote system. What I also like is the ability to use wildcards in TASKKILL.
OS X/Linux – KILL and KILLALL
In OS X and Linux, you have two commands for killing processes: KILL and KILLALL. You have to run these in the terminal window. In order to kill a program, you either have to use the program name or the process ID. There are some ways you can find this info. One way is via the Activity Monitor.
However, that requires the GUI interface. If you are using the command line to kill a process, you can use the command line to find the process info also. Two commands that come to mind are top and ps -ax.
top will give you a list of processes with the PID and the program name also sorted by CPU usage. It’s a quick way to find the process you want to kill. ps -ax will give you a listed sorted by PID and the path of the program. It’s slightly different than top.
Now for killing the program in OS X. You can simply type the following command to kill a specific process:
kill -9 83002
83002 is the Terminal process and the 9 means to kill the process. You can use other numbers like 3, which means Quit, or 6, which means Abort. Mostly, though, you’ll be sticking with 9. You can also use the KILL command to kill all running processes, though you should probably never use this command.
kill -TERM -1
Where KILL is useful for killing one process or all processes, KILLALL is using for killing a group of processes. For example, if you’re running Google Chrome, you might have 10 Chrome processes running. It would be really annoying to use KILL ten times to shut down Chrome. Instead, you could use KILLALL like so:
killall Evernote
or
killall 'Google Chrome'
Note that you have to use the single quotes or anything longer than one word. Also, if the process is not running under your name, but is instead running under root, you have to use the following command:
sudo killall 'Google Chrome'
or
sudo killall -9 'Google Chrome'
Again, the 9 is sending the specific signal to KILL rather than TERM. Sudo is only needed if you get an error about not having permission. Otherwise you can do killall program or killall -9 program. On OS X, the KILLALL command comes in really handy when you can’t force quit a program. You don’t have to know the process ID, which is nice. Just type in the name and all processes will be killed associated with that name.
This article was meant to give you more advanced methods for killing a program in Windows, OS X and Linux. If you have any questions about killing a program using the command line, post a comment and I’ll try to help. Enjoy!