Stop a process using the Terminal in Mac/ Linux OS

problem

You’ve been using a program on your Mac and suddenly crashed and is not working. It is not possible to quit or force quit as the interface is frozen. Running programs are treated as processes by the operating system. One way of terminating such an unwanted process is by going behind the scenes and finding the particular process and send a command to the OS to terminate it.

solution

Each process has certain information, one of them is its id (pid) so it can be uniquely identified. Another piece of information is its user i.e. the user running the particular process.

In order to get the running processes in your Mac or Linux system, you can start the terminal and execute the following command:

ps aux

This will return a tabular representation of the current processes in your system as shown below. It has the following headers:

If you wish to analyze the contents of this command you can simply save the output into a CSV file and subsequently open it in your spreadsheet application and let it present it visually.

To save it in a file, execute the following:

ps aux > /tmp/processes.csv

This will create and save the data in the file processes.csv saved under the /tmp directory.

Tip: the delimiter is one or more spaces; Use that on the import settings so will show the columns correctly

As spreadsheet application, the Numbers is used to open it and looks like:

Now, the point is to locate the process id of the one we’re facing issues with. Let’s suppose I use the Firefox web browser and has crashed.

In terminal, I would like to search by the name of the application in the list of the current processes. For example:

ps aux | grep firefox

This will search by “firefox” in the contents. The output will be (in this case):

As you can see above, there appear to be 2 process IDs (4950 and 19321). The one is the command that I just used to search for the process and the second one is the actual process of Firefox.

To terminate the process, we can do by PID as follows:

kill 4950

If we wish to force kill it we can pass the following argument to the command:

kill -9 4950

The process is now terminated.

If you had multiple processes running, they can also be terminated in batch by passing multiple PIDs to the kill command. For example:

kill -9 4275 749 560 19406 19404 19400 19399

A small detail to notice is that when using grep, it is case sensitive. For example, the following two commands have different outputs.

ps aux | grep safari

and

ps aux | grep Safari

conclusion

In this post we saw one approach to follow when we wish to terminate a process. That is, open the terminal, search for the process by its name and use its process id to kill it.


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x