Skip to content

Latest commit

 

History

History
201 lines (159 loc) · 4.81 KB

File metadata and controls

201 lines (159 loc) · 4.81 KB

Process Management in Linux

Linux provides various commands to manage and monitor processes efficiently. This guide covers the basic commands for running, viewing, and managing processes in Linux, making it easier to understand for beginners.


1. Running Commands in the Background and Foreground:

Background Command:

  • Run a command in the background so you can continue using the terminal for other tasks:
    ping 8.8.8.8 &
    The & at the end sends the command to the background, freeing up the terminal.

Foreground Command:

  • Run a command in the foreground:
    ping 8.8.8.8
    This keeps the terminal occupied until the command completes.

Move a Background Job to the Foreground:

  • Bring a background job to the foreground:
    fg
    This moves the most recent background job to the foreground.

List Jobs Running in the Background:

  • Check which jobs are running in the background:
    jobs
    This shows a list of jobs that are currently running in the background.

Redirect Command Output to the Background:

  • Send a command’s output to the background:
    seq 10000000000000 >/dev/null &
    ping 8.8.8.8 >/dev/null &
    The >/dev/null part sends the output to a special location that discards it, and the & runs it in the background.

2. Viewing Processes with the ps Command:

View Current Shell's Running Processes:

  • See the processes running in your current shell:
    ps

List Processes in Long Format:

  • Get a detailed list of processes:
    ps -l

View All Running Processes (Including CPU and Memory Usage):

  • See a list of all running processes with CPU and memory usage:
    ps -aux

List All Processes:

  • List all processes running on the system:
    ps -e

Display Processes in a Tree Structure:

  • Show processes in a hierarchical tree format:
    ps -axjf

Filter Processes by User (e.g., root):

  • See processes owned by a specific user, like root:
    ps -U root -u root u

Show Processes for a Specific User:

  • Display processes owned by any specific user (e.g., 'name'):
    ps -U user -u user u

3. Monitoring Processes in Real-Time:

View Real-Time System Processes:

  • Monitor active system processes:
    top
    This command shows real-time process activity on your system.

Install htop for Better Monitoring:

  • htop provides a more user-friendly interface to monitor processes:
    • For Debian/Ubuntu:
      sudo apt install htop
    • For RHEL/CentOS:
      sudo yum install htop

Display Processes in a Tree Structure with htop:

  • See processes displayed as a tree:
    htop -t

Get More Detailed Information with htop:

  • htop shows more detailed process information compared to top.

Monitor Processes for a Specific User:

  • View processes for a particular user (e.g., 'name'):
    htop -t -u name

Monitor Processes by Process ID (e.g., 2780):

  • View processes using a specific process ID:
    htop -t -u 2780

4. Killing Processes:

Terminate a Process by ID:

  • Kill a process using its process ID (PID):
    kill 2780

Forcefully Kill a Process:

  • Forcefully terminate a process if it's not responding:
    kill -9 1891

Kill All Instances of a Process (e.g., seq):

  • Terminate all processes of a certain type (e.g., seq):
    • First, install the necessary tool:
      sudo yum install psmisc
    • Then kill all processes with the name seq:
      killall seq

Kill Processes by Name Using Regular Expressions:

  • Terminate all processes matching a specific name (e.g., firefox):
    killall -r firefox

Kill All Processes for a Specific User:

  • Terminate all processes belonging to a specific user:
    killall -u user

Terminate All ping Processes Using pkill:

  • Use pkill to stop all ping processes:
    pkill ping

Kill All Processes for a Specific User (e.g., armour):

  • Terminate all processes for a given user:
    pkill -u user

Kill Only the ping Process for a Specific User:

  • Terminate only the ping process for a particular user (e.g., armour):
    pkill -u user -x ping

Conclusion:

This guide covers the essential commands for managing processes in Linux, from running tasks in the background to terminating processes when necessary. By understanding these basic commands, you can efficiently monitor and control system processes.