How to Manage Processes from the Linux Terminal
The Linux terminal has a number of useful commands that can display running processes, kill them, and change their priority level. This post lists the classic, traditional commands, as well as some more useful, modern ones.
Many of the commands here perform a single function and can be combined — that’s the Unix philosophy of designing programs. Other programs, like htop, provide a friendly interface on top of the commands.
The top command is the traditional way to view your system’s resource usage and see the processes that are taking up the most system resources. Top displays a list of processes, with the ones using the most CPU at the top.
To exit top or htop, use the Ctrl-C keyboard shortcut. This keyboard shortcut usually kills the currently running process in the terminal.
The htop command is an improved top. It’s not installed by default on most Linux distributions — here’s the command you’ll need to install it on Ubuntu:
htop displays the same information with an easier-to-understand layout. It also lets you select processes with the arrow keys and perform actions, such as killing them or changing their priority, with the F keys.
The ps command lists running processes. The following command lists all processes running on your system:
This may be too many processes to read at one time, so you can pipe the output through the less command to scroll through them at your own pace:
Press q to exit when you’re done.
You could also pipe the output through grep to search for a specific process without using any other commands. The following command would search for the Firefox process:
The pstree command is another way of visualizing processes. It displays them in tree format. So, for example, your X server and graphical environment would appear under the display manager that spawned them.
The kill command can kill a process, given its process ID. You can get this information from the ps -A, top or pgrep commands.
Technically speaking, the kill command can send any signal to a process. You can use kill -KILL or kill -9 instead to kill a stubborn process.
Given a search term, pgrep returns the process IDs that match it. For example, you could use the following command to find Firefox’s PID: