In Linux, processes are instances of running programs. Process management is essential for monitoring system performance, debugging, and ensuring the stability of the operating system. This chapter covers the tools and commands for viewing, managing, and controlling processes.
- Process ID (PID): A unique identifier assigned to each process.
- Parent Process ID (PPID): The ID of the process that created the current process.
- Foreground Processes: Processes that run interactively in the terminal.
- Background Processes: Processes that run without user interaction.
- Daemon: A background service typically started at boot time.
- Running: The process is currently executing.
- Sleeping: The process is waiting for a resource.
- Stopped: The process has been stopped by a signal.
- Zombie: The process has completed but is not yet removed by its parent.
- Displays information about running processes.
ps aux # Show all processes with detailed information
ps -ef # Show all processes in full format
- Interactive tool for viewing and managing processes in real-time.
top
- Common Keys in
top
:q
: Quitk
: Kill a processr
: Renice a process
- Provides a user-friendly interface for process management.
sudo apt install htop
htop
- Sends a signal to a process to terminate it.
kill [signal] PID
- Terminate a process:
kill 1234
- Forcefully kill a process:
kill -9 1234
- Terminates processes matching a pattern.
pkill firefox
- Displays jobs started in the current session.
jobs
fg
: Bring a background process to the foreground.bg
: Resume a stopped process in the background.
fg %1
bg %2
- Assigns a priority to a process when starting it.
- Priority ranges from
-20
(highest) to19
(lowest).
nice -n 10 ./script.sh
- Adjusts the priority of a running process.
sudo renice -5 1234
- Provides an overview of system performance.
vmstat 2 5
- Displays CPU and I/O usage.
iostat
- Shows free and used memory.
free -h
- Debugs a process by tracing its system calls and signals.
strace -p 1234
- Shows files opened by processes.
lsof -p 1234
- Controls services and daemons.
- Start a service:
sudo systemctl start apache2
- Stop a service:
sudo systemctl stop apache2
- Check service status:
sudo systemctl status apache2
By the end of this chapter, you should be able to:
- View and monitor processes using
ps
,top
, andhtop
. - Manage processes with
kill
,pkill
,fg
, andbg
. - Adjust process priorities using
nice
andrenice
. - Debug and analyze processes with
strace
andlsof
.
- Move to Chapter 7: Managing User Environment Variables to learn about customizing the user environment in Linux.
- List all processes running on your system and identify their PIDs.
- Start a process in the background and bring it to the foreground using
fg
. - Change the priority of a running process using
renice
. - Use
strace
to debug a simple script and analyze its system calls. - Monitor memory and CPU usage on your system using
vmstat
andiostat
.