I want a script which will restart the defined services on their respective servers. I want to pass parameter as below to the script: eg:
sh execute.sh
Enter your server and service list: [server1:nginx,mysql],[server2:mysql,apache],[server3:mongodb,apache]
By this input the script should verify and start the services on the respective servers. I am able to do this on a single server by declaring variables.
#!/bin/bash
Instance_Name=server1
Service_Name=(nginx php-fpm mysql)
SSH_USER=admin
SSH_IDENT_FILE=~/credentials/user.pem
len=${#Service_Name[*]}
i=0
while [ $i -lt $len ]; do
service=${Service_Name[$i]}
ssh -i $SSH_IDENT_FILE -o StrictHostKeyChecking=no $SSH_USER@$Instance_Name 'service $service restart'
done
Now I don't have an idea to move forward. Please let me know if my question is unclear. Thanks in advance.
forlooping through the name of servers?while read $server; do ... (execute your script with $server parameter) .... done < list_of_serversService_Name=(nginx php-fpm mysql)instead.