Command & Conquer: Monitoring and Optimizing Linux Performance (Part 5)

Keep It Running: System Monitoring and Performance Tuning

Welcome to Part 5 of the Command & Conquer series! By now, you’ve mastered the basics, learned to automate tasks, and explored networking commands. But to truly conquer Linux, you need to ensure your system is running efficiently. This chapter covers essential commands and tools for monitoring and tuning system performance.


Essential Monitoring Commands

1. top and htop: Monitor Processes in Real Time

  • What It Does: Displays active processes, CPU usage, memory usage, and more.

  • Syntax:

    • top: Standard tool, installed by default.

    • htop: User-friendly alternative (install with sudo apt install htop).

  • Example:

    • Run: htop

    • Press F6 to sort by CPU or memory usage.

2. free: Check Memory Usage

  • What It Does: Shows free and used memory (RAM and swap).

  • Syntax: free -h

  • Example:

    • Run: free -h

    • The -h flag makes the output human-readable (e.g., in MB or GB).

3. df: View Disk Space Usage

  • What It Does: Displays available and used disk space for mounted filesystems.

  • Syntax: df -h

  • Example:

    • Run: df -h

    • Look for partitions nearing full capacity.

4. du: Check Directory Sizes

  • What It Does: Shows the size of directories and their contents.

  • Syntax: du -sh <directory>

  • Example:

    • Run: du -sh /var/log

    • The -s flag gives a summary, and -h makes it human-readable.

5. iostat: Monitor CPU, I/O, and Disk Usage

  • What It Does: Reports CPU and I/O statistics.

  • Syntax: iostat

  • Example:

    • Run: iostat

    • Look for high I/O wait times, which can indicate disk bottlenecks.

6. uptime: Check System Load

  • What It Does: Displays how long the system has been running and the average load.

  • Syntax: uptime

  • Example:

    • Run: uptime

    • Output includes the number of users and load averages for the last 1, 5, and 15 minutes.

7. ps and kill: Manage Processes

  • What They Do:

    • ps: Lists active processes.

    • kill: Terminates processes by PID.

  • Example:

    • Run: ps aux | grep firefox to find Firefox’s PID.

    • Kill it with kill <PID>.


Optimization Tools

1. sysctl: Tune Kernel Parameters

  • What It Does: Configures kernel parameters at runtime.

  • Syntax: sudo sysctl <parameter>=<value>

  • Example:

    • Run: sudo sysctl vm.swappiness=10 to reduce swap usage.

2. iotop: Monitor Disk I/O Usage

  • What It Does: Shows disk I/O usage by processes.

  • Syntax: sudo iotop

  • Example:

    • Run: sudo iotop and look for processes with high I/O.

3. nice and renice: Prioritize Processes

  • What They Do: Adjust the priority of running processes.

  • Syntax:

    • nice -n <priority> <command>: Start a process with a priority.

    • renice <priority> -p <PID>: Change the priority of an existing process.

  • Example:

    • Run: renice -10 -p 1234 to give a process higher priority.

Mini Project: Monitor and Optimize Your System

  1. Monitor System Load:

    • Run: uptime and note the load averages.
  2. Identify a Bottleneck:

    • Use top or htop to find resource-hungry processes.
  3. Optimize Disk Space:

    • Run: du -sh /home/* to identify large directories.

    • Delete unnecessary files with rm.

  4. Adjust Priorities:

    • Run a resource-heavy command with lower priority:

        nice -n 19 tar -czf backup.tar.gz /large_directory
      

Why This Matters

System monitoring and tuning ensure your Linux machine stays performant and responsive, even under heavy loads. Master these tools to troubleshoot and optimize like a pro.


What’s Next?

In Part 6, we’ll dive into Linux security essentials, focusing on user management, permissions, and securing your system against threats. Stay Null. Stay Void.🤘