From a bash script, I need to start three processes and put to each of these processes a name, in order to be able to stop them in a later stop command.
# Démarrer les applications eco emploi : backend métier, backend du front (Java), et front (Angular)
demarrer_applications() {
emit "application" "start" "init" "Démarrage des applications ecoemploi" "Soumission des scripts de démarrage"
# Partie métier, backend Java
log "INFO" "Demande de démarrage de l'application métier ecoemploi (Backend Java)"
START_METIER=$(bash -c "exec -a $ECOEMPLOI_METIER_PROCESS_NAME ./submit_ecoemploi_metier_back.sh &")
if [ $? -ne 0 ]; then
emit "application" "start" "fail" "Soumission de ecoemploi backend métier échouée" "$START_METIER"
log "ERROR" "La demande de soumision de l'application métier ecoemploi (Backend Java) a échoué : $START_METIER"
exit $?
fi
# Partie front, backend Java
log "INFO" "Demande de démarrage de l'ihm ecoemploi (Backend Java)"
START_IHM_BACK=$(bash -c "exec -a $ECOEMPLOI_IHM_BACK_PROCESS_NAME ./submit_ecoemploi_ihm_back.sh &")
if [ $? -ne 0 ]; then
emit "application" "start" "fail" "Soumission de ecoemploi backend ihm échouée" "$START_IHM_BACK"
log "ERROR" "La demande de soumision de l'application ihm ecoemploi (Backend Java) a échoué : $START_IHM_BACK"
exit $?
fi
log "INFO" "Demande de démarrage de l'ihm ecoemploi (Front Angular)"
START_IHM_FRONT=$(bash -c "exec -a $ECOEMPLOI_IHM_FRONT_PROCESS_NAME ./submit_ecoemploi_ihm_front.sh &")
if [ $? -ne 0 ]; then
emit "application" "start" "fail" "Soumission de ecoemploi ihm front échouée" "$START_IHM_FRONT"
log "ERROR" "La demande de soumision de l'application ihm ecoemploi (Front Angular) a échoué : $START_IHM_FRONT"
exit $?
fi
log "INFO" "Les demandes de soumission des applications ont été faites."
}
Summarizing that script, it attempts to run these commands:
bash -c "exec -a $ECOEMPLOI_METIER_PROCESS_NAME ./submit_ecoemploi_metier_back.sh &"
bash -c "exec -a $ECOEMPLOI_IHM_BACK_PROCESS_NAME ./submit_ecoemploi_ihm_back.sh &"
bash -c "exec -a $ECOEMPLOI_IHM_BACK_PROCESS_NAME ./submit_ecoemploi_ihm_back.sh &"
Where each of these submit sub-scripts have this form:
#!/bin/bash
source ecoemploi-start-stop-common.sh
cd "$ECOEMPLOI_METIER_HOME"
if [ $? -ne 0 ]; then
emit "application" "start" "fail" "Le répertoire d'installation de l'application métier, $ECOEMPLOI_METIER_HOME, est absent" ""
log "ERROR" "Le répertoire d'installation de l'application métier, $ECOEMPLOI_METIER_HOME, est absent"
exit $?
fi
./start.sh
here, for example, start.sh will start a Java 17 application:
java --add-exports java.base/sun.nio.ch=ALL-UNNAMED \
--add-opens java.base/java.util=ALL-UNNAMED \
--add-opens java.base/java.nio=ALL-UNNAMED \
--add-opens java.base/java.lang=ALL-UNNAMED \
--add-opens java.base/java.lang.invoke=ALL-UNNAMED \
-jar target/application-metier-et-gestion.jar
and ecoemploi-start-stop-common.sh is only here to define few directory locations and two custom functions: emit to send a message to a Kafka topic, and log to log into the console:
#!/bin/bash
#
# Répertoires d'installation de l'application, et noms des processus qui vont être lancés par bash/exec
ECOEMPLOI_HOME="/home/lebihan/dev/Java/comptes-france"
ECOEMPLOI_METIER_HOME="$ECOEMPLOI_HOME/metier-et-gestion/ApplicationMetierEtGestion"
ECOEMPLOI_IHM_FRONT_HOME="$ECOEMPLOI_HOME/web-client/ApplicationEtude/etude"
ECOEMPLOI_IHM_BACK_HOME="$ECOEMPLOI_HOME/web-client/ApplicationEtude"
ECOEMPLOI_METIER_PROCESS_NAME="ecoemploi_backend_metier"
ECOEMPLOI_IHM_BACK_PROCESS_NAME="ecoemploi_ihm_backend"
ECOEMPLOI_IHM_FRONT_PROCESS_NAME="ecoemploi_ihm_front"
# Communication avec Kafka
KAFKA_BOOTSTRAP_SERVER_PORT=9092
BOOTSTRAP_SERVER="localhost:$KAFKA_BOOTSTRAP_SERVER_PORT"
TOPIC_VIE="ecoemploi-statut"
# Logger un message, horodaté
# $1 Sevérité
# $2 Message
log() {
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
if [ "$1" = "ERROR" ]; then
echo "$TIMESTAMP [$1] $2" >&2
else
echo "$TIMESTAMP [$1] $2"
fi
}
# Emettre un évènement dans le $TOPIC_VIE de Kafka
# $1 : Composant
# $2 : Phase : init, start
# $3 : Statut : start, fail
# $4 : description
# $5 : Message
emit() {
# Laisser la ligne ci-dessous non indentée pour que EOF fonctionne.
MESSAGE_EMIT_KAFKA=$(cat << EOF
{
"composant": "$1",
"phase": "$2",
"statut": "$3",
"description": "$4",
"message": "$5"
}
EOF
)
MESSAGE_EMISSION_VERS_KAFKA=$(echo "$MESSAGE_EMIT_KAFKA" | kafka-console-producer.sh --broker-list $BOOTSTRAP_SERVER --topic $TOPIC_VIE)
if [ $? -ne 0 ]; then
log "WARN" "Le message vers le topic $TOPIC_VIE n'a pu être émis : $MESSAGE_EMISSION_VERS_KAFKA"
fi
}
My problem is that in my bash function demarrer_applications(), called after few other ones and producing the last [INFO] log,
2023-04-14 10:01:09 [INFO] Démarrage de Zookeeper puis de Kafka...
2023-04-14 10:01:09 [INFO] Zookeeper et Kafka ont démarré.
2023-04-14 10:01:10 [INFO] Le topic ecoemploi-statut existant sera réutilisé sur Kafka localhost:9092.
2023-04-14 10:01:11 [INFO] Postgresql n'est pas démarré. Démarrage en cours...
2023-04-14 10:01:13 [INFO] Postgresql est prêt.
2023-04-14 10:01:14 [INFO] Demande de démarrage de l'application métier ecoemploi (Backend Java)
do run the wished application, with the command:
bash -c "exec -a $ECOEMPLOI_METIER_PROCESS_NAME ./submit_ecoemploi_metier_back.sh &"
but the & after the ./submit_ecoemploi_metier_back.sh doesn't send that application into the background. And my main script cannot to continue its work and launch the following applications next. It hangs here, waiting for a Ctrl-C.
How shall I submit my submit_ecoemploi_*.sh scripts, in the background, with bash and exec commands? Thanks a lot!
@StéphaneChazelas
I used your solution to adapt my function (I only put here the changed lines):
demarrer_applications() {
# Partie métier, backend Java
NAME=$ECOEMPLOI_METIER_PROCESS_NAME bash -c 'exec -a "$NAME" ./submit_ecoemploi_metier_back.sh &'
NAME=$ECOEMPLOI_IHM_BACK_PROCESS_NAME bash -c 'exec -a "$NAME" ./submit_ecoemploi_ihm_back.sh &'
NAME=$ECOEMPLOI_IHM_FRONT_PROCESS_NAME bash -c 'exec -a "$NAME" ./submit_ecoemploi_ihm_front.sh &'
}
it submits perfectly, and I my application see its three components started and is useable.
but a ps -f returns me that:
UID PID PPID C STIME TTY TIME CMD
lebihan 309213 309207 0 11:18 pts/0 00:00:00 bash
lebihan 322835 2710 21 11:26 pts/0 00:00:04 java -Xmx1G -Xms1G -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -XX:Ma
lebihan 324800 2710 0 11:27 pts/0 00:00:00 /bin/bash /home/lebihan/dev/Java/comptes-france/submit_ecoemploi_metier_back.sh
lebihan 324803 2710 0 11:27 pts/0 00:00:00 /bin/bash /home/lebihan/dev/Java/comptes-france/submit_ecoemploi_ihm_back.sh
lebihan 324804 324800 0 11:27 pts/0 00:00:00 /bin/bash ./start.sh
lebihan 324806 324803 0 11:27 pts/0 00:00:00 /bin/bash ./start.sh
lebihan 324808 324804 99 11:27 pts/0 00:00:47 java --add-exports java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --
lebihan 324809 2710 0 11:27 pts/0 00:00:00 /bin/bash /home/lebihan/dev/Java/comptes-france/submit_ecoemploi_ihm_front.sh
lebihan 324811 324806 52 11:27 pts/0 00:00:07 java --add-exports java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --
lebihan 324812 324809 0 11:27 pts/0 00:00:00 /bin/bash ./start.sh
lebihan 324814 324812 51 11:27 pts/0 00:00:07 ng serve
lebihan 325316 309213 0 11:27 pts/0 00:00:00 ps -f
And I don't see the process names changed.
Then the stop command isn't able to stop the submit_ecoemploi_ihm_front.sh, submit_ecoemploi_ihm_back.sh, submit_ecoemploi_metier_back.sh
exec -aandbash -ctogether, to avoid being suggested to remove theexec -a, or obtain another response that would not do the renaming of the processes currently expected.