Skip to main content

Command Palette

Search for a command to run...

Shell Scripting - 02

Updated
2 min read
Shell Scripting - 02
V

Hello! I'm a student passionate about becoming a Cloud and DevOps Engineer. I have a solid foundation in AWS Cloud, Linux, Docker, and Kubernetes.

In the previous blog, we wrote a basic script to print to the console. This is a continuation of that shell script.

Checking the Node Health using Script

Let's write a script which outputs the node health.

#!/bin/bash
#######################
# Author: Vishal
# Date: 16-06-2024
# this script outputs the node health
# Version : v1
########################
set -x # debug mode
set -e # exits the script when there is error
set -o pipefail # exit when there is pipefail

df -h
free -g
nproc

ps -ef | grep "amazon" | awk -F" " '{print $2}'

# is comment, used for providing information to the reader and bash will simply ignore that line, it is good practice to write comments.
command set -x executes the file in debug mode. (outputs the command so that it is easy to understand).

Process is an instance of a program in execution. Whenever you launch an application, run a program, or execute ha command in the terminal, a process is created. Each process is a separate entity with its own resources. ps command is used to list all the processes. you can use grep to filter the specific processes. ps -ef | grep "amazon"

| (pipe) used to redirect a stream from one program to another program grep print lines that match patterns
awk is text processing command and we printed the second field using the awk command.

Searching Errors in Remote logfile.

curl https://raw.githubusercontent.com/iam-veeramalla/sandbox/main/log/dummylog01122022.log | grep ERROR

using curl command and filter using grep we can get information about ERROR in the logfile.
wget command will download the file.

Finding file and Directory

you can search for a file or directory in linux using find or locate command

find /home/user/ -name script1.sh or locate script1.sh

sudo su

for changing the user su command is used and for performing any administrator task use sudo before command, if you are authorized it will be executed and no if you are not.

su - ec2-user it will switch into another user

sudo su gives you superuser privileges.

This was all about Linux and shell scripting, and I will keep learning more commands in the future.

More from this blog

Vishal Kerketta

14 posts