Thursday, January 30, 2025

Linux 101: Part 8 - System Monitoring and Performance Tuning in Linux


In this eighth part of our Linux 101 series, we will explore System Monitoring and Performance Tuning to help optimize system resources, troubleshoot issues, and improve efficiency.

Why Monitor System Performance?

Monitoring system performance allows you to:

  • Detect and troubleshoot slowdowns.
  • Optimize resource usage.
  • Prevent system crashes and failures.
  • Ensure servers run efficiently.

Essential System Monitoring Commands

1. Checking System Load and CPU Usage

  • View real-time CPU and process usage:
    top
    
  • Enhanced process monitoring (interactive UI):
    htop
    
    (Install htop if not available: sudo apt install htop or sudo dnf install htop)
  • Check CPU load averages:
    uptime
    

2. Checking Memory Usage

  • View memory usage in MB:
    free -m
    
  • Detailed memory usage analysis:
    vmstat 5
    
    (Refreshes every 5 seconds)

3. Checking Disk Usage

  • Check disk space usage:
    df -h
    
  • Check which folders use the most space:
    du -sh /home/*
    

4. Monitoring Running Processes

  • List running processes:
    ps aux
    
  • Find a specific process:
    ps aux | grep processname
    
  • Kill a high-resource-consuming process:
    kill PID
    

5. Checking Network Usage

  • View active network connections:
    netstat -tulnp
    
  • Monitor real-time network usage:
    iftop
    
    (Install with sudo apt install iftop or sudo dnf install iftop)

Performance Tuning in Linux

1. Optimizing CPU Performance

  • Change process priority using nice and renice:
    nice -n 10 command
    
    renice -n -5 -p PID
    
  • Limit CPU usage of a process:
    cpulimit -p PID -l 50
    
    (Install with sudo apt install cpulimit)

2. Managing RAM and Swap Space

  • Clear cached memory:
    sync; echo 3 > /proc/sys/vm/drop_caches
    
  • Check swap usage:
    swapon -s
    
  • Increase swap space:
    sudo fallocate -l 2G /swapfile
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile
    

3. Improving Disk Performance

  • Enable write caching:
    sudo hdparm -W1 /dev/sda
    
  • Check disk health:
    sudo smartctl -a /dev/sda
    
    (Install with sudo apt install smartmontools)

4. Managing Services and Startup Applications

  • List enabled services:
    systemctl list-units --type=service
    
  • Disable unwanted services:
    sudo systemctl disable service-name
    
  • View startup applications:
    systemd-analyze blame
    

Automating Performance Monitoring

You can automate system monitoring with tools like cron jobs and log monitoring:

  • Log system resource usage every 10 minutes:
    crontab -e
    
    Add this line:
    */10 * * * * free -m >> /var/log/memory.log
    
  • Monitor CPU and memory with sar (install with sudo apt install sysstat):
    sar -u 5 10
    

Conclusion

System monitoring and performance tuning are essential for maintaining a stable Linux environment. In the next part, we will explore Backup and Recovery Strategies in Linux to ensure data protection and disaster recovery planning.

Stay tuned for Linux 101: Part 9 – Backup and Recovery Strategies in Linux!

Friday, January 17, 2025

Linux 101: Part 7 - Shell Scripting and Automation in Linux


In this seventh part of our Linux 101 series, we will explore Shell Scripting and Automation, which helps automate repetitive tasks and improve efficiency in Linux system administration.

What is Shell Scripting?

A shell script is a file containing a series of commands that are executed by the shell. It allows users to automate tasks, schedule jobs, and simplify system management.

Why Use Shell Scripting?

  • Automates repetitive tasks.
  • Reduces manual errors.
  • Improves efficiency in system administration.
  • Schedules jobs to run at specific times.

Writing a Basic Shell Script

1. Creating a Script File

Use a text editor to create a new script file:

nano myscript.sh

2. Writing the Script

Add the following lines:

#!/bin/bash
# This is a simple script
echo "Hello, Linux World!"
date

3. Making the Script Executable

chmod +x myscript.sh

4. Running the Script

./myscript.sh

Variables in Shell Scripting

You can store values in variables and use them within the script.

#!/bin/bash
name="Linux User"
echo "Hello, $name!"

Conditional Statements

Shell scripts can use if statements for decision-making.

#!/bin/bash
num=10
if [ $num -gt 5 ]; then
    echo "Number is greater than 5"
else
    echo "Number is 5 or less"
fi

Looping in Shell Scripts

For Loop

#!/bin/bash
for i in {1..5}; do
    echo "Number: $i"
done

While Loop

#!/bin/bash
count=1
while [ $count -le 5 ]; do
    echo "Count: $count"
    count=$((count + 1))
done

Functions in Shell Scripts

Functions help structure scripts into reusable blocks.

#!/bin/bash
hello() {
    echo "Hello, Linux!"
}
hello

Scheduling Tasks with Cron Jobs

Cron jobs allow tasks to be scheduled at specific times.

Editing the Cron Table

crontab -e

Example Cron Job

Run a script every day at midnight:

0 0 * * * /path/to/script.sh

Automating System Maintenance Tasks

Examples of common automation tasks:

  • Backup important files:
    tar -czf backup.tar.gz /home/user/documents
    
  • Clean temporary files:
    rm -rf /tmp/*
    
  • Monitor system resource usage:
    free -m >> memory.log
    

Conclusion

Shell scripting and automation are powerful tools in Linux for improving productivity and system efficiency. In the next part, we will cover System Monitoring and Performance Tuning in Linux to optimize system performance.

Stay tuned for Linux 101: Part 8 – System Monitoring and Performance Tuning in Linux!

Wednesday, January 15, 2025

Linux 101: Part 6 - Process and Job Management in Linux

In the sixth part of our Linux 101 series, we will explore process and job management in Linux. Understanding how to manage running processes is crucial for system administration, troubleshooting, and optimizing system performance.

Understanding Linux Processes

A process is a running instance of a program. Every process in Linux is assigned a unique Process ID (PID) and belongs to a user.

Types of Processes:

  • Foreground processes: Run in the terminal and require user interaction.
  • Background processes: Run in the background without user interaction.
  • Daemon processes: System processes that run in the background (e.g., SSH, cron jobs).

Viewing Running Processes

Use these commands to monitor running processes:

  • View active processes:
    ps aux
    
  • View processes dynamically:
    top
    
    or use an improved version:
    htop
    
  • View processes for a specific user:
    ps -u username
    
  • Find a specific process by name:
    ps aux | grep processname
    

Managing Processes

Killing a Process

  • Terminate a process by PID:
    kill PID
    
  • Force kill a process:
    kill -9 PID
    
  • Kill a process by name:
    pkill processname
    
  • Kill all instances of a process:
    killall processname
    

Changing Process Priority

Every process has a priority value called nice value (ranges from -20 to 19, where -20 is the highest priority and 19 is the lowest).

  • Start a process with a lower priority:
    nice -n 10 command
    
  • Change priority of a running process:
    renice -n 5 -p PID
    
  • Check priority of running processes:
    ps -eo pid,comm,nice
    

Managing Jobs in Linux

Linux allows job control to manage multiple processes in the terminal.

Running and Controlling Jobs

  • Run a command in the background:
    command &
    
  • View background jobs:
    jobs
    
  • Bring a background job to the foreground:
    fg %jobID
    
  • Suspend a running process: Press Ctrl + Z
  • Resume a suspended job in the background:
    bg %jobID
    
  • Terminate a background job:
    kill %jobID
    

Monitoring System Performance

Use these commands to analyze CPU, memory, and process usage:

  • Check system resource usage:
    vmstat 5
    
  • Monitor memory usage:
    free -m
    
  • View disk usage:
    df -h
    
  • Check I/O performance:
    iostat
    

Conclusion

Managing processes and jobs is essential for system stability and efficiency. In the next part, we will explore Linux Shell Scripting and Automation, which will help you automate tasks and improve productivity.

Stay tuned for Linux 101: Part 7 – Shell Scripting and Automation in Linux!