Case in shell scripting.

We use IF LOOP all time, but when it comes to multiple conditions IF Loop gets complicated as you have to write multiple times. CASE saves going through whole if…then…else statements.

#!/bin/bash
echo -n "Enter the Country name "
read Country

echo -n "Captial of $Country is "

case $Country in
        India )
       echo -n "Delhi"
       echo ""
                ;;

    Argentina )
     echo -n "Buenos Aires"
      echo ""
                ;;

    Brazil )
     echo -n "Brasília"
      echo ""
                ;;

    * )
     echo -n "unknown"
                ;;

esac

So, if you enter the country name which is mentioned it will give desired output and if it’s not present It will show unknown as mentioned in script.

[root@server ~]# ./case.sh
Enter the Country name India
Captial of India is Delhi

[root@server ~]# ./case.sh
Enter the Country name Argentina
Captial of Argentina is Buenos Aires

[root@server ~]# ./case.sh
Enter the Country name USA
Captial of USA is unknown
[root@server ~]#

Ansible Playbook to start services

Ansible Playbook to start services

We have already seen how to run Ansible Adhoc Command, now let’s focus on a playbook to start any service on your servers, In the example below I have started crond service but you can use it to suit your needs.

#Ansible Playbook to start service
-
  name: Start Services
  hosts: webservers

  become: true
  tasks:
    - name: Start Services
      service:
       name: crond
       state: started

If you have multiple host groups then replace hosts: webservers with below

 hosts: 
   - webserversUSA
   - webserversASIA
   - webserversEurope

You may have a situation where you want to start multiple services, In that case, you can use ansible loop the example playbook is as below.

#Ansible Playbook to start service
-
  name: Start Services
  hosts: webservers

  become: true
  tasks:
    - name: Start Services
      service:
        name: '{{ item }}'
        state: started
      loop:
        - httpd
        - crond
        - and-so-on
   

Disable host-key checking while using ansible adhoc command

If you want to disable host key checking for ansible adhoc command, first run the the command below.

 [root@server ~]# export ANSIBLE_HOST_KEY_CHECKING=False

Now, once you have disabled host key checking run your ansible ad-hoc command.

 [root@server ~]#  ansible all -i inventory -m shell -a 'uptime'

Run Ansible adhoc command with Sudo

[root@server ~]# ansible all -i inventory.yml -m shell -a 'service crond restart' -K --become

Exit Status in Linux.

What is exit status in the linux?

Whenever you fire a shell command it returns a status to check if that command was successful or not – exit status can be very useful in scripts to make you scripts stable.

Example:-

In the example below, I ran uptime command and it was successful – so when I checked the exit status using echo $? it showed 0.

[root@server ~]# uptime
 05:15:27 up 50 days, 16:53,  1 user,  load average: 0.08, 0.03, 0.08

[root@server ~]# echo $?
0
[root@server ~]#

Now let’s see what happens when command is not successful.

So in the example below, I typed incorrect command and exit status was other than 0 – which indicates error.

[root@server ~]# uptimee
-bash: uptimee: command not found

[root@server ~]# echo $?
127
[root@server ~]#

Practical usage in shell scripting.

#!/bin/bash
cd $1 > /dev/null 2>&1

if [ $? != 0 ]
then
        echo "Directory Not Found" ; exit
fi

echo "Great!"

So, above script terminates if cd command is not successful.
If it’s successful it will execute further code.

[root@server ~]# ./exitstatus.sh /tmp
Great!

[root@server ~]# ./exitstatus.sh /tmpp
Directory Not Found
[root@server ~]#

Restart service automatically and get notified.

So you are running you own webserver which at the times is short of resources and the services like MySQL and HTTPD stops – results your website is down until you get a chance to fix it.

Quick solution would be to implement a script which restarts services at such situations and emails you that the service was restarted – so you can later go and check what was the problem

So below is the script, which will make sure that HTTP , MySQL and Postfix is running all the time – It will check status of the service and if it’s stopped or Dead it will restart it.

How to Implement this Script ?

Setup a cron to run the script every minute – like below.

* * * * * root /home/bob/scripts/servicecheck.sh

Create a file called servicecheck.sh and put the code below.
Just change the email address in the script to yours and you are done.

#!/bin/bash

#####################
#MySQL CHECK
#####################

/sbin/service mariadb status > /var/log/mysqlservicestatus 2>&1
STATUS=$(cat /var/log/mysqlservicestatus | egrep 'dead|failed')

        if [[ $STATUS = "" ]]
        then
/bin/logger -t MYSQL IS RUNNING
else
/sbin/service mariadb restart
/bin/logger -t restarted mysql
echo "Restarted MYSQL" | /bin/mail -s "Restarted MYSQL" you@email.com
fi
echo > /var/log/mysqlservicestatus

#####################
#HTTPD CHECK
#####################

/sbin/service httpd status > /var/log/httpdservicestatus 2>&1
STATUS=$(cat /var/log/httpdservicestatus | egrep 'dead|failed')

        if [[ $STATUS = "" ]]
        then
/bin/logger -t httpd IS RUNNING
else
/sbin/service httpd restart
/bin/logger -t restarted httpd
echo "Restarted HTTPD" | /bin/mail -s "Restarted HTTPD" you@email.com
fi

echo > /var/log/httpdservicestatus


#####################
#POSTFIX CHECK
#####################

/sbin/service postfix status > /var/log/postfixservicestatus 2>&1
STATUS=$(cat /var/log/postfixservicestatus | egrep 'dead|failed')

        if [[ $STATUS = "" ]]
        then
/bin/logger -t postfix IS RUNNING
else
/sbin/service postfix restart
/bin/logger -t restarted postfix
echo "Restarted postfix" | /bin/mail -s "Restarted postfix" you@email.com
fi

You want to monitor other services, just go ahead and add it.