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.
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.
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).
Linux provides several tools to view running processes.
ps: Process StatusThe ps command displays information about active processes.
Basic Usage:$ ps PID TTY TIME CMD 34221 pts/1 00:00:00 bash1763846 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 MonitoringThe top command provides a real-time, interactive view of system processes.
$ topq to quit.htop: Enhanced Process Viewerhtop is an interactive, user-friendly alternative to top. It’s not installed by default but can be added with:
$ sudo apt install htopThen run:
$ htopOnce you’ve identified a process, you can control it using the following commands.
kill: Terminating ProcessesThe 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 NameThe pkill command allows you to kill processes by name instead of PID.
$ pkill process_namekillall: Killing All Instances of a ProcessThe killall command kills all processes with a specific name.
$ killall process_nameYou can manage processes in the background and foreground using the following commands.
Add an ampersand (&) to the end of a command to run it in the background.
$ long_running_command &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
Use the fg command to bring a background process to the foreground.
$ fgLinux allows you to adjust the priority of processes using the nice and renice commands.
nice: Starting a Process with a Custom PriorityThe nice command starts a process with a modified priority. Lower values mean higher priority.
$ nice -n 10 long_running_commandrenice: Changing the Priority of a Running ProcessThe renice command changes the priority of an already running process.
$ renice +10 -p PIDIn addition to top and htop, you can use these tools to monitor system resources:
uptime: System Uptime and LoadThe uptime command shows how long the system has been running and the average load.
$ uptimefree: Memory UsageThe free command displays information about memory usage.
$ free -hdf: Disk UsageThe df command shows disk space usage.
$ df -hProcess 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_nameLet’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.