In this lesson, you will learn how to manage processes on a Linux server. You will learn tools like ps
, top
, and kill
. We will use the nginx
package as our example. To get started, go ahead and install the nginx package with the command below:
sudo
apt
install nginx
Understanding NGINX as a Process
When NGINX is running on your Ubuntu Server, it operates as a process. This process can be viewed, managed, and terminated if necessary, using Linux commands. Knowing how to view and kill processes is an extremely important skill that I want to make sure you understand – so we will cover that in this lesson.
The top utility
The top
command in Linux is an incredibly useful tool for monitoring system performance and managing processes. It provides a real-time, dynamic view of your system’s resource usage, including CPU, memory, and process information. In this lesson, we’ll focus on some essential skills you should have when using top
, with a special emphasis on searching for process names.
Opening top
To start top
, simply type the following command in your terminal: top
Navigating the top
Interface
Once top
is running, you’ll see a display showing a list of the system’s currently running processes, along with information about system resources. The top portion shows overall system statistics, while the bottom portion lists individual processes.
Sorting Processes
By default, top
sorts processes based on CPU usage, but you can sort by other criteria (you must use shift to capitalize each of the letters below):
- Press
M
to sort by memory usage. - Press
P
to sort back by CPU usage. - Press
T
to sort by the time the processes have been running.
Searching for Process Names
One key skill in top
is searching for specific processes by name. This is particularly useful when you’re monitoring a specific application or service. To search for a process:
- Press
L
(capitalL
). This opens a search prompt at the bottom of thetop
interface. - Type the name of the process you’re looking for, like
nginx
. - Press
Enter
.top
will then highlight the first process that matches your search query.
If there are multiple instances of the process, you can press L
and enter the name again to cycle through them.
Killing Processes from top
If you find a process that needs to be terminated, you can do so directly from top
:
- Press
k
. This opens a prompt asking for the PID of the process you want to kill. - Enter the PID of the process. You can find this in the list of processes.
- Press
Enter
.top
will then ask for a signal to send. If you pressEnter
again without typing a signal number, it sendsSIGTERM
(signal 15), which asks the process to terminate gracefully. - To forcefully kill a process (like using
kill -9
), type9
and then pressEnter
.
Forcefully kill all the nginx processes, then exit top using the instructions below.
Exiting top
To exit top
, simply press q
. This will return you to the command line. Check the status of nginx to make sure it was stopped:
sonnyb@ubuntu-server:~$ systemctl status nginx
× nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded
(/lib/systemd/system/nginx.service
; disabled
; vendor preset: enabled
)
Active: failed
(Result: signal
) since Tue
2023-12-05
20:37:57 UTC
; 9s ago Docs: man:nginx
(8)
Process:
74006
ExecStartPre=/usr/sbin/nginx -t -q -g daemon on
; master_process on
;
(code
=exited,
status=0>
Process:
74007
ExecStart=/usr/sbin/nginx -g daemon on
; master_process on
;
(code
=exited,
status=0/SUCCESS
)
Main PID:
74008
(code
=killed,
signal=KILL
) CPU: 20ms