Script Shell qui ne démarre pas toutes les commandes
andyajram
Messages postés155Date d'inscriptionvendredi 24 mai 2013StatutMembreDernière intervention 8 avril 2020
-
7 avril 2020 à 16:46
andyajram
Messages postés155Date d'inscriptionvendredi 24 mai 2013StatutMembreDernière intervention 8 avril 2020
-
8 avril 2020 à 13:23
Bonjour
J'ai un script shell , qui doit vérifier si des services sont démarrés , sinon il les démarre , le problème est que le script ne démarre que les deux premiers services, et ignore les autres suivants , est-ce-que quelqu'un pourrait me dire pourquoi ??
voici le script et le fichier config :
Script a executer:
\#!/bin/bash
#startService
#arg1 service path
#arg2 service command
#arg3 service args
startService(){
echo "Starting......"
echo $1
echo $2
echo $3
cd ${1}
eval ${2} ${3} &
#cd -
#Checking if the service is successfully started
echo "Started!"
}
#Check service method
#arg1 the service configuration string
#arg2 the current node type A or P
checkService(){
#echo $1
#echo $2
#Split the configuration string
IFS=',' read -ra SERVICECONF <<< "$1"
#Get Service PID
PID=$(pgrep -f "${SERVICECONF[4]}")
#echo ${SERVICECONF[1]}
#echo $PID
#Check if the serivce is running
if [ -z $PID ];
then
echo "Service ${SERVICECONF[0]} is NOT running"
#Check if the service is eligible to be started according to the service type and the node type
#NODETYPE == A && service type == A-A (eligible)
#NODETYPE == A && service type == A-P (eligible)
#NODETYPE == P && service type == A-A (eligible)
#NODETYPE == P && service type == A-P (NOT eligible)
#echo +++++++++++++++
#echo $2
#echo ${SERVICECONF[4]}
#echo +++++++++++++++
if [ $2 = "A" -a ${SERVICECONF[5]} = "A-A" ] || \
[ $2 = "A" -a ${SERVICECONF[5]} = "A-P" ] || \
[ $2 = "P" -a ${SERVICECONF[5]} = "A-A" ]; then
echo "Service ${SERVICECONF[0]} is eligible to be restarted"
startService ${SERVICECONF[1]} ${SERVICECONF[2]} ${SERVICECONF[3]}
else
echo "Service ${SERVICECONF[0]} is NOT eligible to be restarted"
fi
else
echo "${SERVICECONF[0]} RUNNING"
if [ $2 = "P" -a ${SERVICECONF[5]} = "A-P" ]; then
echo "${SERVICECONF[0]} is Active-Passive server and shouldn't be running on a passive node"
echo "Killing ${SERVICECONF[0]}"
kill $PID
fi
fi
}
echo "Starting at $(date)"
#check the arguments
if [ "$#" -ne 1 ]; then
echo "The argumants should have the path to the services configuration file only";
exit;
fi
configFile=$1
IFS=''
servicesList=()
#Reading the config file lines into array
while read line; do
configurationList+=($line)
done < $configFile
IFS='=' read -ra NODECONF <<< "${configurationList[0]}"
NODETYPE=${NODECONF[1]}
echo $NODETYPE
for ((i = 1; i < ${#configurationList[@]}; ++i)); do
#echo $i
echo -----------------------
checkService ${configurationList[i]} ${NODETYPE}
done
echo "Done"