Exit from script instead of subshell on missing user

When executing `false || (echo bad; exit 1)`, the call to `exit` won't
exit the script, it will only exit the subshell and the exit code will
be stored in the return code `$?`.

Since this is an error, we have to exit the script properly.
This commit is contained in:
Benedikt Heine 2020-12-27 16:09:07 +01:00
parent cb25be2d12
commit d4251ea5ca
2 changed files with 28 additions and 6 deletions

View File

@ -53,9 +53,20 @@ if [ ! -e $ICINGA2_CONFIG_FILE ]; then
exit 6 exit 6
fi fi
getent passwd $ICINGA2_USER >/dev/null 2>&1 || (echo "Icinga user '$ICINGA2_USER' does not exist. Exiting." && exit 6) if ! getent passwd "$ICINGA2_USER" >/dev/null 2>&1; then
getent group $ICINGA2_GROUP >/dev/null 2>&1 || (echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting." && exit 6) echo "Icinga user '$ICINGA2_USER' does not exist. Exiting."
getent group $ICINGA2_COMMAND_GROUP >/dev/null 2>&1 || (echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting." && exit 6) exit 6
fi
if ! getent group "$ICINGA2_GROUP" >/dev/null 2>&1; then
echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting."
exit 6
fi
if ! getent group "$ICINGA2_COMMAND_GROUP" >/dev/null 2>&1; then
echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting."
exit 6
fi
# Start Icinga 2 # Start Icinga 2
start() { start() {

View File

@ -21,9 +21,20 @@ fi
: ${ICINGA2_LOG_DIR:="@ICINGA2_FULL_LOGDIR@"} : ${ICINGA2_LOG_DIR:="@ICINGA2_FULL_LOGDIR@"}
: ${ICINGA2_CACHE_DIR:="@ICINGA2_FULL_CACHEDIR@"} : ${ICINGA2_CACHE_DIR:="@ICINGA2_FULL_CACHEDIR@"}
getent passwd $ICINGA2_USER >/dev/null 2>&1 || (echo "Icinga user '$ICINGA2_USER' does not exist. Exiting." && exit 6) if ! getent passwd "$ICINGA2_USER" >/dev/null 2>&1; then
getent group $ICINGA2_GROUP >/dev/null 2>&1 || (echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting." && exit 6) echo "Icinga user '$ICINGA2_USER' does not exist. Exiting."
getent group $ICINGA2_COMMAND_GROUP >/dev/null 2>&1 || (echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting." && exit 6) exit 6
fi
if ! getent group "$ICINGA2_GROUP" >/dev/null 2>&1; then
echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting."
exit 6
fi
if ! getent group "$ICINGA2_COMMAND_GROUP" >/dev/null 2>&1; then
echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting."
exit 6
fi
if [ ! -e "$ICINGA2_INIT_RUN_DIR" ]; then if [ ! -e "$ICINGA2_INIT_RUN_DIR" ]; then
mkdir -m 755 "$ICINGA2_INIT_RUN_DIR" mkdir -m 755 "$ICINGA2_INIT_RUN_DIR"