In this module, we will explore how to interact with the system using Bash scripts. This includes executing system commands, managing system resources, and retrieving system information. By the end of this module, you will be able to write scripts that can perform various system-level tasks efficiently.

Key Concepts

  1. Executing System Commands: Learn how to run system commands from within a Bash script.
  2. Managing System Resources: Understand how to manage system resources like CPU, memory, and disk space.
  3. Retrieving System Information: Learn how to gather information about the system, such as uptime, user sessions, and hardware details.

Executing System Commands

Running Commands

You can execute any system command from within a Bash script by simply writing the command as you would in the terminal.

#!/bin/bash
# A simple script to display the current date and time

echo "The current date and time is:"
date

Capturing Command Output

To capture the output of a command into a variable, use the $(command) syntax.

#!/bin/bash
# A script to capture the current user and display a message

current_user=$(whoami)
echo "Hello, $current_user! Welcome to the system."

Practical Example

#!/bin/bash
# A script to check disk usage and alert if usage exceeds a threshold

threshold=80
current_usage=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

if [ "$current_usage" -gt "$threshold" ]; then
  echo "Warning: Disk usage is above $threshold%. Current usage is $current_usage%."
else
  echo "Disk usage is under control. Current usage is $current_usage%."
fi

Managing System Resources

CPU and Memory Usage

You can use commands like top, htop, ps, and free to monitor CPU and memory usage.

#!/bin/bash
# A script to display CPU and memory usage

echo "CPU Usage:"
top -b -n1 | grep "Cpu(s)"

echo "Memory Usage:"
free -h

Disk Space

Use the df command to check disk space usage.

#!/bin/bash
# A script to display disk space usage

echo "Disk Space Usage:"
df -h

Retrieving System Information

Uptime

The uptime command provides information about how long the system has been running.

#!/bin/bash
# A script to display system uptime

echo "System Uptime:"
uptime

User Sessions

The who command shows who is currently logged into the system.

#!/bin/bash
# A script to display current user sessions

echo "Current User Sessions:"
who

Hardware Information

Commands like lscpu, lsblk, and lshw provide detailed information about the system's hardware.

#!/bin/bash
# A script to display CPU and block device information

echo "CPU Information:"
lscpu

echo "Block Devices:"
lsblk

Practical Exercises

Exercise 1: System Health Check Script

Objective: Write a script that checks the system's health by displaying CPU usage, memory usage, disk space usage, and system uptime.

Solution:

#!/bin/bash
# System Health Check Script

echo "System Health Check"

echo "CPU Usage:"
top -b -n1 | grep "Cpu(s)"

echo "Memory Usage:"
free -h

echo "Disk Space Usage:"
df -h

echo "System Uptime:"
uptime

Exercise 2: User Session Monitor

Objective: Write a script that monitors user sessions and alerts if a specific user logs in.

Solution:

#!/bin/bash
# User Session Monitor Script

target_user="john_doe"
current_users=$(who | awk '{ print $1 }')

if echo "$current_users" | grep -q "$target_user"; then
  echo "Alert: $target_user has logged in."
else
  echo "$target_user is not logged in."
fi

Common Mistakes and Tips

  • Mistake: Forgetting to make the script executable.
    • Tip: Use chmod +x script_name.sh to make your script executable.
  • Mistake: Not handling command errors.
    • Tip: Use conditional statements to check the success or failure of commands.
  • Mistake: Hardcoding values that may change.
    • Tip: Use variables and configuration files to make your scripts more flexible.

Conclusion

In this module, you learned how to interact with the system using Bash scripts. You can now execute system commands, manage system resources, and retrieve system information. These skills are essential for writing effective and efficient Bash scripts that can automate various system-level tasks. In the next module, we will explore automation and scheduling with cron jobs.

© Copyright 2024. All rights reserved