Process management is a crucial aspect of working with Linux. It involves understanding how to view, control, and manage processes running on a system. This section will cover the following key concepts:
- Understanding Processes
- Viewing Processes
- Controlling Processes
- Background and Foreground Processes
- Practical Exercises
Understanding Processes
A process is an instance of a running program. Each process in Linux has a unique Process ID (PID). Processes can be in various states such as running, sleeping, stopped, or zombie.
Key Concepts:
- PID (Process ID): A unique identifier for each process.
- PPID (Parent Process ID): The PID of the process that started the current process.
- UID (User ID): The user ID of the process owner.
- GID (Group ID): The group ID of the process owner.
Viewing Processes
Linux provides several commands to view and monitor processes. The most commonly used commands are ps
, top
, and htop
.
ps
Command
The ps
command is used to display information about active processes. It has various options to customize the output.
Basic Usage:
Common Options:
ps aux
: Displays all processes with detailed information.ps -ef
: Another format to display all processes.
Example:
This command will display a list of all running processes with details such as user, PID, CPU usage, memory usage, and command.
top
Command
The top
command provides a dynamic, real-time view of running processes.
Basic Usage:
Key Features:
- Displays system summary information.
- Allows sorting and filtering of processes.
- Interactive commands to control the display.
htop
Command
htop
is an interactive process viewer similar to top
, but with a more user-friendly interface.
Basic Usage:
Key Features:
- Color-coded display.
- Easy navigation and process management.
- Customizable interface.
Controlling Processes
Linux allows you to control processes using various commands. The most common operations are killing, stopping, and resuming processes.
kill
Command
The kill
command is used to send signals to processes. The most common signal is SIGKILL
(signal 9), which forcefully terminates a process.
Basic Usage:
Example:
This command will send the default SIGTERM
signal to the process with PID 1234.
killall
Command
The killall
command is used to kill all processes with a specific name.
Basic Usage:
Example:
This command will terminate all instances of the Firefox browser.
pkill
Command
The pkill
command is similar to killall
but allows more flexible matching criteria.
Basic Usage:
Example:
This command will terminate all processes whose command line matches the pattern python
.
Background and Foreground Processes
Processes can run in the foreground or background. Foreground processes occupy the terminal until they complete, while background processes run independently.
Running a Process in the Background
To run a process in the background, append an &
at the end of the command.
Example:
This command will run the sleep
command in the background.
Bringing a Background Process to the Foreground
Use the fg
command to bring a background process to the foreground.
Example:
This command will bring the first background job to the foreground.
Practical Exercises
Exercise 1: Viewing Processes
- Use the
ps aux
command to list all running processes. - Identify the PID of the
bash
process.
Solution:
Exercise 2: Killing a Process
- Start a
sleep
process in the background. - Use the
ps
command to find its PID. - Kill the
sleep
process using thekill
command.
Solution:
Exercise 3: Using top
and htop
- Run the
top
command and observe the output. - Run the
htop
command and explore its features.
Solution:
Conclusion
In this section, you learned about process management in Linux, including how to view, control, and manage processes. You also practiced using commands like ps
, top
, htop
, kill
, and fg
. Understanding these concepts is essential for effective system administration and troubleshooting. In the next section, we will explore scheduling tasks with cron
.
Linux Mastery: From Beginner to Advanced
Module 1: Introduction to Linux
Module 2: Basic Linux Commands
- Introduction to the Command Line
- Navigating the File System
- File and Directory Operations
- Viewing and Editing Files
- File Permissions and Ownership
Module 3: Advanced Command Line Skills
- Using Wildcards and Regular Expressions
- Piping and Redirection
- Process Management
- Scheduling Tasks with Cron
- Networking Commands
Module 4: Shell Scripting
- Introduction to Shell Scripting
- Variables and Data Types
- Control Structures
- Functions and Libraries
- Debugging and Error Handling
Module 5: System Administration
- User and Group Management
- Disk Management
- Package Management
- System Monitoring and Performance Tuning
- Backup and Restore
Module 6: Networking and Security
- Network Configuration
- Firewall and Security
- SSH and Remote Access
- Intrusion Detection Systems
- Securing Linux Systems
Module 7: Advanced Topics
- Virtualization with Linux
- Linux Containers and Docker
- Automating with Ansible
- Linux Kernel Tuning
- High Availability and Load Balancing