Last active
January 27, 2024 17:57
-
-
Save andarilhoz/e5617051224d6d678a17a9a2addb3ebe to your computer and use it in GitHub Desktop.
Desligar o servidor automaticamente quando não tiverem jogadores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cd /home/ubuntu/palworld | |
# Execute o comando para obter a lista de jogadores e capturar possíveis erros | |
OUTPUT=$(docker-compose run --rm rcon ShowPlayers 2>&1) | |
# Filtra linhas indesejadas | |
CLEANED_OUTPUT=$(echo "$OUTPUT" | grep -v "Creating palworld_rcon_run") | |
# Verifica se ocorreu um erro na execução do comando | |
if echo "$CLEANED_OUTPUT" | grep -q "ERROR"; then | |
echo "Erro ao executar o comando. Possivelmente, máquina está ligando." | |
STOP_INSTANCE=false | |
else | |
# Conta o número de jogadores | |
NUM_PLAYERS=$(echo "$CLEANED_OUTPUT" | wc -l) | |
NUM_PLAYERS=$((NUM_PLAYERS-1)) # Ajusta para subtrair o cabeçalho, se houver | |
if [ "$NUM_PLAYERS" -le 0 ]; then | |
echo "Nenhum jogador online." | |
STOP_INSTANCE=true | |
else | |
echo "Há $NUM_PLAYERS jogadores online." | |
echo "Lista de Jogadores:" | |
echo "$CLEANED_OUTPUT" | cut -d ',' -f 1 | |
STOP_INSTANCE=false | |
fi | |
fi | |
# Se não houver jogadores online ou ocorreu um erro, parar o container do Docker e desligar a máquina | |
if [ "$STOP_INSTANCE" = true ]; then | |
# Encontra o ID do container que está executando a imagem especificada | |
CONTAINER_ID=$(docker ps -q --filter ancestor=jammsen/palworld-dedicated-server:latest) | |
# Verifica se um container foi encontrado | |
if [ -n "$CONTAINER_ID" ]; then | |
echo "Parando o container do Docker: $CONTAINER_ID" | |
docker stop $CONTAINER_ID | |
else | |
echo "Nenhum container encontrado para a imagem especificada." | |
fi | |
echo "Desligando a máquina..." | |
sudo shutdown -h now | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment