0

I am trying to ssh into multiple aws nodes and start a service(it contains a list of services) if it is stopped. Following is the script that I have tried, I am completely new to the shell script, please suggest me if this is a better way to achieve what I am trying to do.

#!/bin/bash

service_1="service_1"
service_2="service_2"
service_3="service_3"
service_4="service_4"
service_5="service_5"
service_6="service_6"

check_service()
{
service_status=$(pgrep "$1" | wc -l)
if [ "$service_status" -ne 1 ]
then
 echo "starting service $1"
 "$1 start"
else
  echo "stopping service $1"
  "$1 stop"
fi
}

    # Reading the node.list line by line 
while read -r line
do
    {
    echo "running ssh on $line"
    # specify the directory of the pem file location
    ssh -i /directory/keyname.pem ec2-user@"$line"
# Passing the different service variables to check_service method 
check_service "$service_1"
check_service "$service_2"
check_service "$service_3"
check_service "$service_4"
check_service "$service_5"
check_service "$service_6"
 } < /dev/null
done < "node.list"
Auto-learner
  • 1,411
  • 7
  • 26
  • 43
  • If these services are managed by `systemd` or `initV`, you can use `systemctl` or `service` – ingroxd Mar 03 '19 at 17:00
  • These are not manged by systemctl or initV – Auto-learner Mar 03 '19 at 17:05
  • 1
    You can add them: [systemd](https://www.linuxunbound.com/2014/04/adding-custom-services-systemd/), [initV](https://askubuntu.com/questions/299371/correct-way-to-install-a-custom-upstart-service). After adding them, you will have a standard way for management. You can check them with `systemctl status name` instead of creating a function. – ingroxd Mar 03 '19 at 17:16
  • 1
    Try http://shellcheck.net/ to get basic diagnostics, you have a syntax error and lack proper quoting. – tripleee Mar 04 '19 at 04:10

0 Answers0