
Modifiqué un script de shell que encontré aquí: https://github.com/Slympp/ConanLinuxScript
Pero estoy teniendo problemas con la función "conan_stop". El script simplemente termina después
exec kill -SIGINT $pid
El script envía el comando Kill con éxito, pero después de eso simplemente termina sin código de error ni nada parecido.
Todas las variables del script se definen anteriormente en el archivo.
Función completa
function conan_stop {
pid=$(ps axf | grep ConanSandboxServer-Win64-Test.exe | grep -v grep | awk '{print $1}')
if [ -z "$pid" ]; then
echo "[$(date +"%T")][FAILED] There is no server to stop"
else
if [ "$discordBotEnable" = true ]; then
echo "[$(date +"%T")][SUCCESS] Discord bot is enabled"
if [ -n "$botToken" ] && [ -n "$channelID" ]; then
secLeft=$(($delayBeforeShutdown * 60))
while [ $secLeft -gt "0" ]; do
minLeft=$(($secLeft / 60))
echo "[$(date +"%T")][WAIT] Server will be shut down in $minLeft minutes"
python3 $discordScript $botToken $channelID "Servern kommer stängas ner om " $minLeft "minuter."
secLeft=$(($secLeft - 60))
sleep 60
done
python3 $discordScript $botToken $channelID "Servern stängs nu ner."
else
echo "[$(date +"%T")][ERROR] No Discord botToken or channelID found"
fi
fi
echo "[$(date +"%T")][SUCCESS] Existing PIDs: $pid"
exec kill -SIGINT $pid
isServerDown=$(ps axf | grep ConanSandboxServer-Win64-Test.exe | grep -v grep)
cpt=0
while [ ! -z "$isServerDown" ]; do
echo "[$(date +"%T")][WAIT] Server is stopping..."
((cpt++))
sleep 1
isServerDown=$(ps axf | grep ConanSandboxServer-Win64-Test.exe | grep -v grep)
done
echo "[$(date +"%T")][SUCCESS] Server stopped in $cpt seconds"
if [ "$discordBotEnable" = true ]; then
echo "[$(date +"%T")][SUCCESS] Discord bot is enabled"
if [ -n "$botToken" ] && [ -n "$channelID" ]; then
python3 $discordScript $botToken $channelID "Servern stängdes ner efter $cpt sekunder."
else
echo "[$(date +"%T")][ERROR] No Discord botToken or channelID found"
fi
fi
fi
}
Respuesta1
exec
reemplaza el caparazóncon el comando dado, como la exec()
llamada al sistema. Cuando el comando (el kill
, aquí) se detiene, el shell ya no existe, por lo que no hay forma de que el script continúe.
Las dos excepciones son 1) cuando exec
se dan redirecciones, en cuyo caso simplemente las aplica en el shell actual, y 2) cuando el comando no se puede ejecutar, en cuyo caso exec
genera un error y devuelve un código de salida falso.
Entonces, exec kill ...
es casi lo mismo que kill ... ; exit
. No es exactamente lo mismo, pero en este caso se acerca bastante.