Mac’s Terminal application provides a great tool that gives us the power to perform many different tasks. A couple of those tasks are viewing and killing processes that may be running on your system.
Whether you want to see what is running on your Mac or there is a process you need to end because it is causing your system to slow down, they can both be handled through Mac Terminal using the ps or top and kill commands, respectively.
My name is Eric, and as a software engineer, I use command line interfaces like Terminal on a daily basis. Knowing how to view and kill processes running on a system is an integral part of what I do, and if you want to learn, I can show you how.
Keep reading below, and I will go over these very useful commands and then touch on some of the reasons we must sometimes use them.
- View Processes via Mac Terminal
- Kill Processes via Mac Terminal
- Why Would I Need to Kill a Process?
View Processes via Mac Terminal
Sometimes we just want to see what is going on with our system, so we might want to view the currently running processes. Other times we may already know that there is a process running that we need to kill or end. There are two ways that we can view them. Let’s take a look.
The top Command
The top command is a real-time command that acts and displays much like Activity Monitor, which is provided with your macOS. By default, it shows a running list of applications and processes running and puts them in order of CPU usage.
It also displays memory usage and many other stats in a live display in your Terminal window that updates in real-time. Use the following steps to run this command and view your system’s processes.
Step 1: Open Mac Terminal using your preferred method.
Step 2: Run the top command; just type it in as shown below and hit return.
Step 3: Expand the Terminal window size so you can see all the information.
As shown above, you will see the processes running along with all of the data, including the PID. The PID is the process id, which will be important later in the article when we learn how to kill a process.
The ps Command
The ps command is another method to view the currently running processes. While this command is not as exciting because it doesn’t show the real-time changes and doesn’t have as much information, it is sometimes a better choice because it is easier to read.
The information stays in one place, and you see the name of the process along with the PID, which is what you are looking for most of the time. This is my preferred method of viewing the processes running on my system. Use the following steps to try it.
Step 1: Open Mac Terminal.
Step 2: Type in the command as shown below and hit return.
Step 3: See the output as shown in the example below.
The number in the second column is the PID of the process or command shown in the last column.
The list of processes will scroll by on the screen very quickly, and if there are a lot of them, they may not all fit on the screen, so you may not have the beginning of the list still on the screen.
You can pipe the output to the more command, which will pause the output when the screen fills up. Type the command as shown below.
ps -ef | more
Hit the return key, and the output will stop as soon as the screen fills. To see the next screen’s worth of output, hit the spacebar. You can continue hitting the space bar until the output reaches the end.
Another solution to a long output list is to direct the output to a file. This can be nice if you wish to save the data for any reason. Just type the command as shown below.
ps -ef > psdata.txt
I have used a filename called psdata.txt here, but you can call the output file anything you would like. Now, if I want to view the file, I can use an editor like nano to look at the output without worrying about it going away from the screen.
You could also go to Finder on your desktop and find the file and open it with a text editor there as well. Use the method that you are most comfortable with.
Kill Processes via Mac Terminal
Now that you know how to use Mac Terminal to view the processes running on your system, you can kill (or end) them if needed. Just follow the quick steps below, and you will be able to stop any process running on your system.
Step 1: Open Mac Terminal
Step 2: Find the PID
To kill the process, you will need to first find the PID or process id of that application. You can use the commands shown above to find it, but if you are having trouble and know the process or application name, you can use the ps command in combination with grep.
For example, if I want to terminate the Calculator application from Terminal, I would use the ps command in combination with grep as shown below to see Calculator’s PID.
ps -ef | grep Calculator
The PID in the above example is located in the second column and is 9407. You will also see the process you are running (grep Calculator) included in the results. Ignore that one because it is the grep command that you just ran.
Step 3: Kill the process
Use the kill command to end the process.
kill -9 9407
Hit enter, and it will end the process. Notice the -9; this tells it to end the process without asking for confirmation. You can see more information about the kill command by typing man kill.
You won’t see any kind of response after running the command unless there is an error or it can’t find the process. To verify if it has actually ended the process, run the ps command again, and you should only see your grep command on the screen.
Why Would I Need to Kill a Process?
Now that you know how to view and kill processes from your Mac Terminal, you might be wondering why you would need to do this. There are a variety of reasons. You may have a process that takes up a lot of CPU time, and you want to stop it.
Another situation may be that you have background processes running that you want to get rid of. These processes may not necessarily appear as applications on your desktop, so you need a way to end them. The method above is a great way to do that.
Sometimes when you are testing a new application or script, you might run into a bug or error that causes a runaway process, an infinite loop, or the application freezes, and there is no other way to end it other than to kill it using the method above.
In other cases, you might just find it more convenient to kill a process from Terminal rather than use the application monitor or reboot your system.
Below are some frequently asked questions about viewing and killing processes with Mac Terminal.
What if I get a message saying that I am not the owner?
In some cases, if the process is owned by another user who logged into your system or the root user, you may not be able to kill it with your own user id, but you can do it as the root user with the sudo command. Just type it as follows.
sudo kill -9 <PID>
You will need to have admin privileges on the system under your login, and you will need to enter your password when prompted.
Is it safe to kill processes from Mac Terminal?
For the most part, killing processes on your Mac is safe. You are not deleting files or anything like that. It is possible you could kill a process that affects how your system functions, but in most cases, a simple reboot will restore your system and the process that you killed.
Why do I see multiple processes with the application name that I want to kill?
It is possible that an application running on your system will have multiple processes that make it work. Therefore you may see multiple processes containing a portion of the name of the process you want to stop.
You may or may not need to kill all of them; it just depends on the application and how it works. You may need to research further to determine what needs to be done.
If you are looking to view processes running on your Mac from Terminal, the top and ps commands are what you will want to use. The kill command, along with the PID, is used to stop any of the processes that need to be ended.
As usual, please let me know if you have any questions or comments. I would love to hear from you!