add stop polling on server shutdown

This commit is contained in:
Nayan
2025-09-04 00:07:22 +00:00
parent 5274b58ba8
commit b355898d0a

14
bot.py
View File

@@ -209,12 +209,16 @@ async def _stop_server_internal(ctx, servername, method):
except Exception as e: except Exception as e:
await ctx.send(f"Failed to send stop command to screen: {e}") await ctx.send(f"Failed to send stop command to screen: {e}")
# Even if command fails, attempt cleanup # Even if command fails, attempt cleanup
# Check if the server is still running every second until the server stops or the timeout is reached
for i in range(30):
check_screen_cmd = f"screen -ls | grep {servername}"
process = subprocess.Popen(check_screen_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
stdout, stderr = process.communicate()
if not stdout:
break
await asyncio.sleep(1)
# Give the server some time to shut down gracefully
if method == "stop":
await asyncio.sleep(15) # Minecraft servers can take a while to save and stop
else: # forcestop
await asyncio.sleep(5) # Give it a few seconds for Ctrl+C to register
# --- Cleanup --- # --- Cleanup ---
if active_server_pipe_task: if active_server_pipe_task: