Mac上的终端(Terminal)应用程序可以帮助识别在后台运行的辅助服务和进程可能会导致Mac 运行速度变慢。(Mac to slow down.)终端(Terminal)应用程序帮助用户通过命令行界面进入 macOS。在终端(Terminal)中键入命令时,请确保空格、字符和大写字母准确无误。
注意(Note):对于本指南,我们使用的是运行macOS Big Sur的(macOS Big Sur)MacBook。
如何使用 Mac 终端应用程序终止进程(How to Kill a Process Using the Mac Terminal Application)
使用终端(Terminal)终止进程所需的基本步骤:
- 备份您的文件和文件夹
- 启动终端应用程序
- 查看正在运行的进程列表
- 找到要关闭的进程
- (Kill)使用终端(Terminal)命令终止进程
1.备份您的文件和文件夹 (Backup Your Files and Folders )
使用 Time Machine 备份您的数据、文件和文件夹(backup your data, files and folders using Time Machine)可能是个好主意,以避免由于意外删除而不得不从备份中恢复您的Mac 。Time Machine易于设置和使用。
使用Time Machine,您还可以回到过去,查看文件在最近的样子。查看上面的链接,阅读我们关于设置Time Machine的文章。
2.启动终端应用程序(2. Launch the Terminal Application)
您可以使用多种选项在Mac上打开(Mac)终端(Terminal),包括使用键盘快捷键(keyboard shortcut)、启动板(Launchpad)或从应用程序(Applications)文件夹打开终端。(Terminal)
- 要使用键盘快捷键启动终端,请按(Terminal)Command + Spacebar打开Spotlight。搜索终端(Terminal)并双击它以打开应用程序。
- 您也可以通过Launchpad打开(Launchpad)终端(Terminal)。选择Dock 上的Launchpad ,选择(Launchpad)其他(Other)文件夹,然后选择终端(Terminal)。
- 或者,导航到菜单栏上的Go ,选择(Go)Utilities,然后双击Terminal以启动它。
将打开一个标准终端(Terminal)窗口,显示您上次登录的日期和时间,以及您将在其中键入要执行的命令的命令提示符。您还将看到默认为Home Folder的当前(工作)目录。
3.查看当前正在运行的进程列表 (3. View the List of Currently Running Processes )
如果您想快速查看Mac上的所有活动进程,您可以打开活动监视器并查看按(Activity Monitor)CPU使用率排序的每个进程。您还可以在“内存”选项卡中查看按(Memory)RAM使用量排序的相同进程。
按照以下步骤在终端(Terminal)中查看进程。
- 在您刚刚启动的终端(Terminal)窗口中键入top ,您将看到当前正在运行的进程列表以及它们正在消耗的资源。
- 您还可以键入 ps -ax 列出正在运行的进程及其PID(PIDs)、运行时间、进程名称和位置。
4. 找到你想要关闭的进程(4. Find the Process You Want to Close)
您可以根据PID或CMD列中的名称快速从进程列表中识别出一个进程。您可以使用以下方法来查找PID:
- 检查活动(Activity)监视器并向下滚动以在终端(Terminal)窗口中找到相关进程。
- 使用grep命令通过其PID或名称查找进程并过滤掉所需的信息。您可以将 grep 命令与 ps ax 命令一起使用,以仅列出您要杀死的进程。
- 例如,您可以键入ps ax | grep GarageBand来查找GarageBand ,而不是在(GarageBand)Mac上数百个当前正在运行的进程中寻找它。
您可能看到的结果如下所示:
此示例显示Garageband的PID为 547 以及启动Garageband的文件夹。
5. 使用终端命令终止进程(5. Kill the Process Using a Terminal Command)
您可以使用Command + Option + Esc组合键强制( Esc )退出应用程序,但(Force Quit the application)强制退出应用程序(Force Quit Applications)窗口中仅列出单个应用程序,而不是Mac上运行的所有进程。
- 要杀死不需要的进程,请记下该进程的PID,然后在终端中键入kill <Process PID number> 按Enter 键(Enter),该过程将立即退出。例如,要终止 GarageBand(GarageBand),我们将键入kill 547。
- 或者,您可以使用killall命令按进程名称终止进程并终止包含其名称的所有进程。例如,killall GarageBand将终止所有名称中包含GarageBand的进程。
注意(Note):在使用 killall 命令之前,请仔细检查进程。
强制退出无响应的应用程序(Force Quit Unresponsive Applications)
(Terminal)如果程序无法响应或意外挂起,(program fails to respond or unexpectedly hangs)终端是在 macOS 中强制退出进程或程序的快速方法。希望(Hopefully)上述步骤可以解决您的问题。如果问题再次出现,请考虑更新应用程序或寻找替代应用程序。
本指南是否(Did)帮助您消除Mac上的麻烦进程?在下面的评论中与我们分享。
How to Kill a Process Using Terminal in macOS
The Terminal app on thе Mac can help identify which ancillary services and processes running in the background may be causing yоur Mac to slow down. The Terminal application helps the user get inside macOS via a command line interface. While typing in the commands in Terminal ensure that spaces, characters and capitalizations are accurate.
Note: For this guide, we’re using a MacBook running macOS Big Sur.
How to Kill a Process Using the Mac Terminal Application
The basic steps required to kill a process using Terminal:
- Backup your files and folders
- Launch Terminal application
- View the list of running processes
- Find the process you want to close
- Kill the process using a Terminal command
1. Backup Your Files and Folders
It may be a good idea to backup your data, files and folders using Time Machine, to avoid having to restore your Mac from a backup because of accidental deletion. Time Machine is easy to set up and use.
With Time Machine, you can also go back in time and see what a file looked like in the recent past. Check out the link above to read our article on setting up Time Machine.
2. Launch the Terminal Application
There are several options you can use to open Terminal on your Mac, which includes using a keyboard shortcut, Launchpad, or open Terminal from the Applications folder.
- To launch Terminal using a keyboard shortcut, press Command + Spacebar to open Spotlight. Search for Terminal and double-click on it to open the application.
- You can also open Terminal through the Launchpad. Select Launchpad on the Dock, select Other folder and then select Terminal.
- Alternatively, navigate to Go on the menu bar, select Utilities and then double-click Terminal to launch it.
A standard Terminal window will open showing the date and time you last logged in, and the command prompt where you’ll type the command you want to execute. You’ll also see the current (working) directory that defaults to your Home Folder.
3. View the List of Currently Running Processes
If you want to quickly view all the active processes on your Mac, you can open the Activity Monitor and see each process sorted by its CPU usage. You can also see the same processes ranked by the amount RAM used in the Memory tab.
Follow the steps below to view processes in Terminal.
- Type top in the Terminal window you just launched and you’ll see a list of processes currently running and the resources they’re consuming.
- You can also type ps -ax to list the running processes along with their PIDs, elapsed time running, process name and location.
4. Find the Process You Want to Close
You can quickly identify a process from the process list based on the PID or the name in the CMD column. You can use the following methods to find the PID:
- Check the Activity monitor and scroll down to find the relevant process in the Terminal window.
- Use the grep command to find a process by its PID or its name and filter out the desired information. You can use the grep command together with the ps ax command to list only the process you want to kill.
- For example, you can type ps ax | grep GarageBand to find GarageBand instead of looking for it among the hundreds of currently running processes on your Mac.
The result you may see will look something like this:
This example shows that Garageband has a PID of 547 and also the folder where Garageband was launched from.
5. Kill the Process Using a Terminal Command
You can Force Quit the application by using the Command + Option + Esc key combination, but only individual apps are listed in the Force Quit Applications window instead of all the processes running on your Mac.
- To kill an unwanted process, note down the PID of the process and then type kill <Process PID number> in Terminal. Press Enter and the process will exit immediately. For example, to kill GarageBand, we’ll type kill 547.
- Alternatively, you can use the killall command to kill a process by its name and kill all the processes containing its name. For example, killall GarageBand will terminate all processes that have GarageBand in their name.
Note: Check the processes carefully before using the killall command.
Force Quit Unresponsive Applications
Terminal is a quick way to force quit a process or program in macOS if the program fails to respond or unexpectedly hangs. Hopefully, the steps mentioned above resolve your issue. If the issue resurfaces, consider updating the application or finding an alternative application.
Did this guide help you kill a troublesome process on your Mac? Share with us in a comment below.