Simple Shell Script Tutorial for System Information Display...

Table of contents

Last Blog Review →
In the last blog we understood, how to automate running a script using crontab saving lot of time for running script manually and saving the data separately. As cron does this job with automation for you and you just need to check the log file at last day of week to see if everything is in place.
Scenario →
Write a shell script which will display server’s multiple information to know the current status of the system.
Solution →
Let’s go to our ubuntu machine
Lets go to our scripts folder and create a shell script to display system's multiple information
i-002978a646c30dd8b (EC2) ------------------------- ubuntu@ip---:~/scripts$ vim disk_space.sh ubuntu@ip---:~/scripts$ cat disk_space.sh #!/bin/bash print_line() { local len=${1:-120} local char="${2:-*}" printf '%*s\n' "$len" '' | tr ' ' "$char" } echo "------Welcome $(whoami) to your first Shell Scripting Project------" echo echo "==> Current date is : $( date | awk '{print $1, $2, $3, $6}')" echo "==> Current time is : $( date | awk '{print $4, $5}')" echo print_line echo "The uptime of the system is : $(uptime)" echo print_line echo "System is in running state since : $(uptime -p)" echo print_line echo "The system was UP at : $(uptime -s)" echo print_line echo "Status of the users who are logged in are : " && w echo print_line echo "All the logins made to the machine are : " && last echo print_line echo "Latest login made to the machine : " && last | head -1 echo print_line echo "Disk Space available on the filesystem : " print_line 40 "-" df -h | xargs | awk '{print "Total Space : " $9, " || Free_Space/Available_Space : " $10 "/" $11, " || Used_Memory : " $12}' echo print_line echo "Summary of RAM Usage : " print_line 40 "-" free -h | xargs | awk '{print "Total_RAM_Available : " $8, " || Memory_Used : " $9, " || Unsed_Memory : " $10}' echo print_line echo "Running Process in CPU : " print_line 40 "-" top -b | head -1 echo print_line ubuntu@ip---:~/scripts$ chmod 700 disk_space.sh ubuntu@ip---:~/scripts$ ./disk_space.sh ------Welcome ubuntu to your first Shell Scripting Project----------------------------------------------------------------- ==> Current date is : Tue Aug 17 2024 ==> Current time is : 22:40:46 UTC ******************************************************************************************************************************** The uptime of the system is : 22:40:46 up 2:21, 1 user, load average: 0.00, 0.00, 0.00 ******************************************************************************************************************************** System is in running state since : up 2 hours, 21 minutes ******************************************************************************************************************************** The system was UP at : 2023-08-17 20:18:51 ******************************************************************************************************************************** Status of the users who are logged in are : 22:40:46 up 2:21, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ubuntu pts/0 18.206.107.28 21:29 5.00s 0.17s 0.00s w ******************************************************************************************************************************** All the logins made to the machine are : ubuntu pts/0 18.206.107.28 Tue Aug 17 21:29 still logged in ubuntu pts/0 18.206.107.29 Tue Aug 17 21:28 - 21:28 (00:00) ubuntu pts/0 18.206.107.28 Tue Aug 17 20:21 - 21:28 (01:07) reboot system boot 5.19.0-1025-aws Tue Aug 17 20:18 still running wtmp begins Tue Aug 17 20:18:53 2024 ******************************************************************************************************************************** Latest login made to the machine : ubuntu pts/0 18.206.107.28 Tue Aug 17 21:29 still logged in ******************************************************************************************************************************** Disk Sapce available on the filesystem : ----------------------------------------- Total Space : 7.6G || Free_Space/Available_Space : 1.6G/6.0G || Used_Memory : 21% ******************************************************************************************************************************** Summary of RAM Usage : ----------------------- Total_RAM_Available : 965Mi || Memory_Used : 201Mi || Unsed_Memory : 366Mi ******************************************************************************************************************************** Running Process in CPU : ------------------------- top - 22:40:46 up 2:21, 1 user, load average: 0.00, 0.00, 0.00 ********************************************************************************************************************************
Conclusion →
In the this blog we understood, how to write a shell script to display system’s multiple information, where we are displaying following information -
Uptime of system
System’s running state
System’s UP at
Status of user’s logging in to system
All the logins made to machine
Latest login made to the machine
Disk space available
Ram Usage
Running Process
Subscribe to my newsletter
Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mihir Suratwala
Mihir Suratwala
Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.