Linux System Administration

0 of 77 lessons complete (0%)

Overview of Permissions & Process Management

Linux Process Management

You don’t have access to this lesson

Please register or sign in to access the course content.

Linux Process Management: Status, Killing, Priority

Introduction to Process Control

A “process” in Linux is an instance of a running program. Effective process management is crucial for maintaining system performance, troubleshooting issues, and ensuring applications run smoothly. This guide will delve into how to inspect the status of processes, how to terminate them when necessary, and how to adjust their priority to optimize system resources.

1. Process Status

Understanding the status of running processes is essential for monitoring system health and troubleshooting.

Viewing Processes (`ps`)

The `ps` (process status) command shows a snapshot of the current processes.

ps aux
    # Common options:
    # a: Show processes for all users
    # u: Display user-oriented format
    # x: Show processes not attached to a terminal

**Example Output Columns (simplified):**

  • `USER`: The user who owns the process.
  • `PID`: Process ID (a unique number for each process).
  • `%CPU`: CPU utilization of the process.
  • `%MEM`: Memory utilization of the process.
  • `STAT`: Current state of the process.
  • `COMMAND`: The command that started the process.

Interactive Process Monitoring (`top`, `htop`)

For real-time, interactive monitoring, `top` and `htop` are invaluable.

top
    # Press 'q' to quit.
    # Press 'k' to kill a process (then enter PID and signal).

`top` provides a dynamic view of running processes, sorted by CPU usage by default.

sudo apt install htop  # Install htop if not present
    htop
    # Press 'F9' to kill, 'F7'/'F8' to nice/renice, 'q' to quit.

`htop` is an enhanced, more user-friendly version of `top` with color-coding and easier navigation.

Common Process States (`STAT` column)

  • `R` (Running): The process is currently running or ready to run.
  • `S` (Sleeping): The process is waiting for an event (e.g., I/O, user input).
  • `D` (Uninterruptible Sleep): The process is in deep sleep, usually waiting for I/O, and cannot be killed.
  • `Z` (Zombie): The process has terminated, but its entry still exists in the process table because its parent hasn’t collected its exit status.
  • `T` (Stopped): The process has been stopped by a signal (e.g., Ctrl+Z).

2. Killing Processes

Sometimes, a process might become unresponsive or consume too many resources. You can terminate processes using `kill`, `killall`, or `pkill`.

The `kill` Command (by PID)

The `kill` command sends a signal to a process specified by its Process ID (PID).

kill [signal] PID

**Common Signals:**

  • `15` (SIGTERM): The default signal. Requests the process to terminate gracefully (allows it to save work and clean up).
  • `9` (SIGKILL): Forces the process to terminate immediately. It cannot be ignored by the process. Use with caution!

# Example: Gracefully terminate a process with PID 12345
    kill 12345

    # Example: Force-terminate a stubborn process with PID 54321
    kill -9 54321

To find a PID, you typically use `ps aux | grep [process_name]`.

The `killall` Command (by Name)

The `killall` command terminates processes by their name, rather than PID.

killall [signal] process_name
# Example: Gracefully terminate all instances of 'firefox'
    killall firefox

    # Example: Force-terminate all 'nginx' processes
    sudo killall -9 nginx

Be careful with `killall`, as it will terminate *all* processes matching the name.

The `pkill` Command (More Flexible by Name/Criteria)

`pkill` is similar to `killall` but offers more powerful pattern matching and filtering capabilities.

pkill [options] pattern
# Example: Gracefully terminate processes whose name contains 'apache'
    pkill apache

    # Example: Force-terminate processes owned by 'www-data' user
    sudo pkill -9 -u www-data

    # Example: Terminate processes running on a specific terminal (tty)
    pkill -t pts/0

3. Process Priority (`nice`, `renice`)

Process priority determines how much CPU time a process gets relative to other processes. In Linux, this is controlled by a “niceness” value.

Niceness Values

The “niceness” value ranges from **-20 (highest priority)** to **+19 (lowest priority)**. * A lower niceness value means the process is “less nice” to other processes and gets more CPU time. * A higher niceness value means the process is “nicer” and gets less CPU time. * The default niceness for new processes is `0`.

Starting a Process with a Specific Priority (`nice`)

Use the `nice` command to launch a new process with a non-default niceness value.

nice -n [niceness_value] command_to_run
# Example: Start a CPU-intensive script with low priority (+10)
    nice -n 10 ./my_long_script.sh

    # Example: Start a process with higher priority (-5) - requires sudo for negative values
    sudo nice -n -5 ./my_important_app

Changing Priority of a Running Process (`renice`)

Use the `renice` command to change the niceness of an already running process.

renice [niceness_value] -p PID
# Example: Change the niceness of process with PID 12345 to +15 (lower priority)
    renice 15 -p 12345

    # Example: Change the niceness of process with PID 67890 to -10 (higher priority) - requires sudo
    sudo renice -10 -p 67890

You can also `renice` by user or group. For example, `renice 10 -u youruser` would make all processes owned by `youruser` have a niceness of 10.