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:

  1. Understanding Processes
  2. Viewing Processes
  3. Controlling Processes
  4. Background and Foreground Processes
  5. 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:

ps

Common Options:

  • ps aux: Displays all processes with detailed information.
  • ps -ef: Another format to display all processes.

Example:

ps aux

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:

top

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:

htop

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:

kill <PID>

Example:

kill 1234

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:

killall <process_name>

Example:

killall firefox

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:

pkill <pattern>

Example:

pkill -f python

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:

sleep 100 &

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:

fg %1

This command will bring the first background job to the foreground.

Practical Exercises

Exercise 1: Viewing Processes

  1. Use the ps aux command to list all running processes.
  2. Identify the PID of the bash process.

Solution:

ps aux | grep bash

Exercise 2: Killing a Process

  1. Start a sleep process in the background.
  2. Use the ps command to find its PID.
  3. Kill the sleep process using the kill command.

Solution:

sleep 100 &
ps aux | grep sleep
kill <PID>

Exercise 3: Using top and htop

  1. Run the top command and observe the output.
  2. Run the htop command and explore its features.

Solution:

top
htop

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.

© Copyright 2024. All rights reserved