Background
I have a discord bot for my personal discord server with my friends, and have a command with it to start our Minecraft server.
The Problem
I need the command to start the Minecraft server to be run as a completely separate process, but nothing I have been trying has worked.
The Code
Python code
from discord import Interaction
from discord.ext import commands
...
class Minecraft(commands.Cog):
"""Minecraft commands"""
...
@commands.slash_command(help="Starts a minecraft server")
async def startmc(self, interaction: Interaction, server: str = "blox-smp"):
"""Starts a minecraft server"""
# Check for valid server name
if not server in ["blox-smp"]:
await interaction.response.send_message("Invalid server name")
return
# Send status message
await interaction.response.send_message(f"Starting minecraft server {server}...")
# Start the server
if server == "blox-smp":
# Need to start a separate process running the command: /var/mc-servers/blox_smp_1/run.sh y
...
run.sh
#!/bin/bash
# Get script directory
# Thanks Dave Dopson: https://stackoverflow.com/a/246128/20382671
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Get current working directory
CURRENT_WD="$PWD"
# Change to script directory
cd $SCRIPT_DIR
# Get from-program
FROM_PROGRAM=$1
if [ -z $FROM_PROGRAM ]
then
FROM_PROGRAM="n"
fi
# Check active screen list for screen
sudo -u mc-user screen -ls | grep -i "blox_smp_1_screen"
if [ $? -ne 0 ]
then
# Update paper by running my pthon script
python3 update-paper.py
# Start the screen if it wasnt running before
sudo -u mc-user screen -dmS blox_smp_1_screen /usr/lib/jvm/java-19-openjdk-amd64/bin/java -Xms10G -Xmx20G -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -Dterminal.jline=false -jar paper.jar --nogui
fi
if [ "$FROM_PROGRAM" = "n" ]
then
sudo -u mc-user screen -r blox_smp_1_screen
fi
# Change back to working directory
cd $CURRENT_WD
I've Tried...
subprocess.Popen()
multiprocessing.Process(daemon=true)
os.spawnl(os.P_NOWAIT)
- Using
&
at the end of my commands
Edit 1
When I manually run the /var/mc-servers/blox_smp_1/run.sh y
command, the server starts without the screen being connected. When I exit the terminal I ran run.sh
in, the server stays on.
Any help is appreciated.