In the final part of the scripting series, we will be a looking at a real world example of a system health script you can run in your production environment.
This type of script is a powerful tool for both new and seasoned Linux users, offering a quick and automated snapshot of a system's health. It combines key administrative tasks - like checking uptime, disk usage, system load, user activity, and log data - into a single, readable report. Whether you're a student learning Bash, a sysadmin managing servers, or a DevOps engineer monitoring resources, this script saves time, reduces manual effort, and builds a habit of proactive system checks. It’s not just a script - it’s a hands-on learning experience and a real-world productivity booster.
Write the script in VIM and save it.
Save the script.
The tee command in Linux is used to read from standard input and write to both standard output and one or more files simultaneously. It's especially useful when you want to see the output of a command on the screen but also save it to a file.
This is a well-organized Bash script designed to generate a system health report and log it to a timestamped file. Below is a breakdown of the script section by section, along with explanations of the commands and their purposes.
Description of what is happening in the script:
🧾 1. Define Variables
LOG_DIR="$HOME/sys_logs"
DATE_STR=$(date +%F_%H-%M-%S)
LOG_FILE="$LOG_DIR/report_$DATE_STR.log"
HOSTNAME=$(hostname)
CURRENT_USER=$(whoami)
• LOG_DIR: Sets the directory where logs will be stored.
• DATE_STR: Gets current date and time in YYYY-MM-DD_HH-MM-SS format.
• LOG_FILE: Constructs the full path of the log file.
• HOSTNAME, CURRENT_USER: Capture system hostname and the user running the script.
📁 2. Prepare Directory and Log File
mkdir -p "$LOG_DIR"
touch "$LOG_FILE"
• Ensures the log directory exists, and creates an empty log file.
🖥 3. System Info
Prints basic system information like:
• Current user
• Hostname
• Home directory
• Default shell
• Current $PATH
All outputs are shown on-screen and saved to the log file using tee -a.
⏳ 4. System Uptime
uptime -p | tee -a "$LOG_FILE"
• Shows how long the system has been running in human-readable form.
💾 5. Disk Usage
df -h | grep -v tmpfs | tee -a "$LOG_FILE"
• Displays disk usage excluding tmpfs (temporary filesystems).
📂 6. Top 5 Largest Directories in /
du -ah / 2>/dev/null | sort -rh | head -n 5 > /tmp/top_dirs.tmp
cat /tmp/top_dirs.tmp | tee -a "$LOG_FILE"
• Lists the 5 largest files/directories starting from root.
• Redirects errors (e.g. permission denied) to /dev/null.
👥 7. Math with expr (User Count)
total_users=$(who | wc -l)
users_plus_five=$(expr $total_users + 5)
• Counts logged-in users.
• Adds 5 to that count using expr.
🔢** 8. Math with (( )) (Log Files Count)**
log_count=$(ls -1 /var/log | wc -l)
log_times_three=$(( log_count * 3 ))
• Counts how many files are in /var/log.
• Multiplies that count by 3 using arithmetic expansion.
🧮 9. Floating-point Math with bc (Load Average)
load_string=$(cat /proc/loadavg)
load1=$(echo "$load_string" | cut -d ' ' -f1)
load5=$(echo "$load_string" | cut -d ' ' -f2)
avg_load=$(echo "scale=2; ($load1 + $load5)/2" | bc)
• Gets CPU load averages (1 and 5 minute).
• Calculates their average using bc, which handles floating-point math.
✅ 10. Exit Status Check
ls /etc/passwd > /dev/null
status=$?
• Runs a harmless command and captures its exit status.
• Useful for testing if commands succeed (status 0) or fail (non-zero).
🏁 11. Script Exit
exit 0
• Clean exit from the script, signaling success.
✅ Key Concepts Demonstrated
• Variables and string substitution
• Date formatting
• File and directory operations
• Redirection and piping
• Use of tee to log and display output
• Integer and floating-point math
• Process status ($?)
• Basic use of utilities like df, uptime, du, bc
Apply the necessary permission to the script so it can be executed.
If we do a ls –ltrh we see that the script has been created and also it is in green colour – meaning the permissions have been applied to the script and therefore it is now executable.
Output of the script once it has been executed.
Following the execution of the script, a directory has been created with log files.
If we do a ls –ltrh and see what is inside the directory.
We can see that a log file has been created following the execution of the script.
If we were to go into the log file with VIM, it basically displays the same information as the output – following the execution of the script.
Connect with me on LinkedIn
#30DaysLinuxChallenge #RedHatEnterpriseLinux
#CloudWhistler #CloudEngineer #Linux
#DevOps #RedHat #OpenSource
#CloudComputing #Automation
#CloudEngineer #SysAdmin #ITSecurity #TechTips #BusinessIT #Leadership