How to unregister batch server before shutdown?

Hello

I have an autoscale setup for our batch servers so when they are created if there are to many in-queue batch jobs.

When the new batch node gets created, I managed to set it up and register the node into the scheduller DB and the node start processing jobs.

The autoscale policy in place discard any nodes when they are not in use (a CPU Usage % threshold).

What we need to do, is run a script before the server dropped so the node is removed from the scheduler.

This is the script we use to unregister the batch node:

#!/bin/bash 
#
###########
# Script para borrar nodo de batch
#
# Calipso 2022
#
##########

#set -o nounset                              # Treat unset variables as an error
KALTURA_FUNCTIONS_RC=/opt/kaltura/bin/kaltura-functions.rc
if [ ! -r "$KALTURA_FUNCTIONS_RC" ];then
        OUT="ERROR: Could not find $KALTURA_FUNCTIONS_RC so, exiting.."
        echo -e $OUT
        exit 3
fi

. $KALTURA_FUNCTIONS_RC

trap 'my_trap_handler "${LINENO}" $?' ERR

CONFIG_DIR=/opt/kaltura/app/configurations
if [ -r $CONFIG_DIR/system.ini ];then
        . $CONFIG_DIR/system.ini
else
        echo -e "${BRIGHT_RED}ERROR: Missing $CONFIG_DIR/system.ini. Exiting..${
NORMAL}"
        exit 1
fi

# Se elimina el batch del monit para que no lo cargue de nuevo
rm -f $BASE_DIR/app/configurations/monit/monit.d/enabled.batch.rc

# Se apaga el monit en el servidor
service kaltura-monit stop >> /dev/null 2>&1

# Se apaga el servicio kaltura-batch
/etc/init.d/kaltura-batch stop >/dev/null 2>&1

#Borro registro de la BD buscando por el nombre
BATCH_HOSTNAME=`hostname`
mysql -N -h$DB1_HOST -u$DB1_USER -p$DB1_PASS $DB1_NAME -P$DB1_PORT -e "delete from scheduler where name='$BATCH_HOSTNAME'"

It does work correctly if I run the script manually.

I have been trying to run the script using service units on systemd so it runs before shutdown but it fails to remove the node from the scheduler, I guess is because the network is not available when trying to execute the query.

I would like to know where we can place such a script so it get run before shutdown and before the network is deactivated.

Any help will be appreciated.

Regards,

Felipe