Chapter 10: Process Management

In this chapter, we’ll explore how Linux manages processes. You’ll learn how to view, control, and manage running programs using commands like ps, top, kill, and more. By the end of this chapter, you’ll be able to monitor and manage processes like a pro.


Why Learn About Processes?

Processes are the running instances of programs on your system. Whether you’re troubleshooting a misbehaving application, monitoring system performance, or managing background tasks, understanding processes is essential for effective system administration.


1. What is a Process?

A process is a program that’s currently running on your system. Each process has: 
- A unique Process ID (PID)
- A parent process that started it. 
- A user and groupthat own it. 
- A state (e.g., running, sleeping, stopped).


2. Viewing Processes

Linux provides several tools to view running processes.

ps: Process Status

The ps command displays information about active processes.

Basic Usage:
$ ps
   PID TTY          TIME CMD
 34221 pts/1    00:00:00 bash
1763846 pts/1    00:00:00 ps

This shows processes running in the current terminal.

View All Processes: $ ps aux This displays all processes running on the system, including those owned by other users.

top: Real-Time Process Monitoring

The top command provides a real-time, interactive view of system processes.

$ top
  • Key Features:
  • Sorts processes by CPU usage by default.
  • Updates every few seconds.
  • Press q to quit.

htop: Enhanced Process Viewer

htop is an interactive, user-friendly alternative to top. It’s not installed by default but can be added with:

$ sudo apt install htop

Then run:

$ htop

3. Controlling Processes

Once you’ve identified a process, you can control it using the following commands.

kill: Terminating Processes

The kill command sends a signal to a process, usually to stop it.

Basic Usage
$ kill PID 

This sends the TERMsignal, asking the process to terminate gracefully.

Forceful Termination
$ kill -9 PID 
This sends the KILL signal, forcing the process to stop immediately.

pkill: Killing Processes by Name

The pkill command allows you to kill processes by name instead of PID.

$ pkill process_name

killall: Killing All Instances of a Process

The killall command kills all processes with a specific name.

$ killall process_name

4. Background and Foreground Processes

You can manage processes in the background and foreground using the following commands.

Running a Process in the Background

Add an ampersand (&) to the end of a command to run it in the background.

$ long_running_command &

Moving a Process to the Background

If a process is already running, you can pause it and move it to the background:
 1. Press Ctrl + Z to pause the process. 
2. Use the bg command to resume it in the background. $ bg

Bringing a Process to the Foreground

Use the fg command to bring a background process to the foreground.

$ fg

5. Process Priorities

Linux allows you to adjust the priority of processes using the nice and renice commands.

nice: Starting a Process with a Custom Priority

The nice command starts a process with a modified priority. Lower values mean higher priority.

$ nice -n 10 long_running_command

renice: Changing the Priority of a Running Process

The renice command changes the priority of an already running process.

$ renice +10 -p PID

6. Monitoring System Resources

In addition to top and htop, you can use these tools to monitor system resources:

uptime: System Uptime and Load

The uptime command shows how long the system has been running and the average load.

$ uptime

free: Memory Usage

The free command displays information about memory usage.

$ free -h

df: Disk Usage

The df command shows disk space usage.

$ df -h

Process monitoring tools like htop and glances offer interactive interfaces and real-time resource usage graphs, making it easier to identify and manage runaway processes or memory leaks. 

systemd, the prevalent service manager in modern Linux distributions, allows you to start, stop, and enable services using systemctl. By combining these tools, you gain comprehensive visibility into your system’s processes and can effectively manage services in production environments.

# Install htop
$ sudo apt install htop

# Install glances
$ sudo apt install glances

# Example usage:
$ htop
$ glances
$ systemctl start service_name
$ systemctl stop service_name
$ systemctl enable service_name

Practice Time!

Let’s put your new skills to the test: 
1. Use ps aux to view all running processes. 
2. Use top to monitor system performance in real time. 
3. Start a process in the background and bring it to the foreground. 
4. Use killto terminate a process.


That’s it for Chapter 10! You’ve now learned how to view, control, and manage processes in Linux. In the next chapter, we’ll dive into the environment—understanding environment variables and shell configuration. Until then, practice managing processes to become more comfortable with system administration.


Prev: Chapter 9 | Next: Chapter 11