Creating Custom Commands in Unix/Linux
Working smart means bringing maximum production from your work. In day to day life, Unix/Linux users encounter some certain set of commands which need to be executed quite frequently. Executing these commands on a regular basis may become quite irritating sometimes.
Unix/Linux systems allow us to create custom commands to cope up with this situation. Instead of executing the same set of commands every time, we can put these commands in a bash script and create a custom command. In today’s tutorial, we will create a custom command for monitoring Unix/Linux operating system with one command.
Steps To Create Custom Command in Unix/Linux
- Create a bash script
- Making the bash script executable
- Moving the bash script to /usr/bin location
Creating a Bash Script to Monitor Unix/Linux Operating Systems
We are creating a bash file with name monitoring.
vim monitoring
#!/bin/bash echo -e "\e[30;46m***** CURRENT LOAD *****\e[0m" echo Current load on Server: `cat /proc/loadavg | awk '{print $1}'` echo "" # -HostName Information echo -e "\e[30;46m8***** HOSTNAME INFORMATION *****\e[0m" hostname echo "" # -File system disk space usage: echo -e "\e[30;46m***** FILE SYSTEM DISK SPACE USAGE *****\e[0m" df -Th echo "" # -Free and used memory in the system: echo -e "\e[30;46m ***** FREE AND USED MEMORY *****\e[0m" free -h echo "" # -System uptime and load: echo -e "\e[30;46m***** SYSTEM UPTIME AND LOAD *****\e[0m" uptime echo "" # -Logged-in users: echo -e "\e[30;46m***** CURRENTLY LOGGED-IN USERS *****\e[0m" who echo "" # -Top 5 processes as far as memory usage is concerned echo -e "\e[30;46m***** TOP 5 MEMORY-CONSUMING PROCESSES *****\e[0m" ps -eo %mem,%cpu,comm --sort=-%mem | head -n 6 echo "" echo -e "\e[1;32mDone.\e[0m"
Making a bash script executable
chmod +x monitoring
Moving the bash script to “/usr/bin” location
mv monitoring /usr/bin
Executing the command on Unix/Linux shell
monitoring
The output of the command
Creating custom commands will “cut corners” too hard way of executing the same set of commands again and again and also brings some sort of creativity and fun into Unix/Linux.
This brings an end to this tutorial. Visit back to find more cool stuff.
Cheers!
Recent Comments