Process Management Fundamentals Common Commands

Process Management Fundamentals Common Commands

Process Management Concepts

Process management in the Linux command line involves controlling and monitoring running processes. A process is an instance of a program executing within a computer program. Key concepts include:

  • Process ID (PID): A unique identifier for a process.
  • Process States: Running, waiting, stopped, zombie, and more.
  • Process Hierarchy: Parent-child relationships and process groups.

Common Commands

The following commands are essential for managing processes in the Linux shell.

1. ps

Purpose: Displays information about running processes.
Usage: ps [options]
Examples:

  • ps -ef shows all processes in full detail.
  • ps -p 1234 views the process with PID 1234.
    Key Parameters:
  • -e: Show all processes.
  • -f: Full format output.
  • -p <PID>: Specify a process by PID.

2. top

Purpose: Real-time monitoring of processes.
Usage: top
Features:

  • Updates dynamically.
  • Shows CPU and memory usage.
  • Allows sorting by CPU, memory, or user.
    Commands:
  • q: Quit.
  • k: Kill a process.

3. kill

Purpose: Terminate processes.
Usage: kill [signal] [PID]
Signals:

  • SIGTERM (15): Requested termination.
  • SIGKILL (9): Forceful termination.
    Examples:
  • kill 1234 ends process 1234.
  • kill -9 5678 forces termination.

4. wait

Purpose: Wait for a process to complete.
Usage: wait [PID]
Features:

  • Blocks until the specified process finishes.
  • Useful for scripting to ensure subsequent commands run only after a process exits.

5. fg and bg

Purpose: Bring a background process to the foreground or vice versa.
Usage: fg or bg
Examples:

  • bg %1 puts process 1 in the background.
  • fg resumes a background process.

6. pkill and pgrep

Purpose: Search and terminate processes.
Commands:

  • pkill [signal] [pattern]: Terminate processes matching a pattern.
  • pgrep [pattern]: Find processes matching a pattern.
    Examples:
  • pkill -u user kills all processes owned by user user.
  • pgrep -f "grep" finds processes using grep.

7. nice and renice

Purpose: Adjust process priority.
Usage: nice [command] or renice [priority] [PID]
Features:

  • nice lowers the priority of a process to reduce CPU usage.
  • renice adjusts the priority of a running process.

8. trap

Purpose: Execute a command when a process exits.
Usage: trap COMMAND EXIT_STATUS [PID]
Examples:

  • trap 'echo "Process exited"' EXIT
  • trap 'echo "Error: $LINENO"' 1
    Note: Useful for logging or cleanup tasks.

Advanced Topics

  • Process Groups: Groups of related processes (e.g., fg -n 2 to bring multiple background processes to the foreground).
  • Background Execution: Using & to run commands in the background.
  • Monitoring: htop is an alternative to top with a more user-friendly interface.

These commands form the foundation of process management in the Linux shell, enabling users to control, monitor, and troubleshoot processes effectively.