Webdev

In-Depth Process Management

Process management in Linux involves monitoring and controlling the execution of processes on a system. Here’s a deeper dive into key concepts and commands:

Viewing Processes

  1. ps (Process Status):
    • Purpose: Displays information about active processes.

    • Common Options:

      • -e or -A: Show all processes.
      • -f: Full format listing, including command line arguments.
      • aux: Combined options for detailed output.
    • Examples:

      ps -e         # List all processes
      ps -ef        # Full format listing
      ps aux        # Detailed process information
      
  2. top:

    • Purpose: Provides a dynamic, real-time view of system processes.
    • Features: Displays CPU, memory usage, process IDs, and more.
    • Navigation: Use arrow keys to scroll, q to quit.

    • Example:
      top           # Open the top command interface
      
  3. htop:

    • Purpose: An enhanced, interactive version of top with a user-friendly interface.
    • Features: Graphical display of CPU, memory usage, process list, and easier process management.
    • Installation: May need to be installed via package manager (e.g., sudo apt install htop).

    • Example:
      htop          # Open the htop command interface
      

Managing Processes

  1. kill:
    • Purpose: Sends signals to processes, commonly used to terminate them.
    • Common Signals:
      • SIGTERM (15): Graceful termination (default).
      • SIGKILL (9): Forceful termination (non-catchable).
    • Syntax:
      kill PID       # Send default SIGTERM to process with PID
      kill -9 PID    # Forceful termination using SIGKILL
      
    • Example:
      kill 1234      # Terminate process with PID 1234
      
  2. pkill:
    • Purpose: Kills processes by name or other criteria.
    • Syntax:
      pkill [options] name
      
    • Common Options:
      • -f: Match against the full command line.
    • Example:
      pkill firefox  # Terminate all processes with the name 'firefox'
      
  3. nice and renice:
    • Purpose: Adjusts the priority of processes to manage system load.
    • nice: Starts a new process with a specified priority.
      • Syntax:
        nice -n priority command
        
      • Example:
        nice -n 10 myscript.sh  # Start 'myscript.sh' with priority 10
        
    • renice: Changes the priority of an existing process.
      • Syntax:
        renice -n priority -p PID
        
      • Example:
        renice -n 5 -p 1234  # Change the priority of process with PID 1234 to 5
        

Process States and Identifiers

  1. Process ID (PID): Unique identifier for each process.
    • Find PID: Use ps or top commands.
  2. Parent Process ID (PPID): Identifier of the parent process that spawned a process.

  3. Process States:
    • R (Running): Process is currently executing.
    • S (Sleeping): Process is waiting for an event.
    • Z (Zombie): Process has finished execution but still has an entry in the process table.
    • T (Stopped): Process has been stopped (e.g., by a signal).

Understanding these tools and concepts allows you to effectively monitor, manage, and control processes on a Linux system, ensuring optimal performance and resource utilization.

Go back to topics page [[Linux Basics]].