-3

I am currently working on a discord bot using discord.py. One of the functions is starting up a minecraft server. The code for the function is as follows:

@commands.command()
async def startmc(self, ctx):
    await ctx.send("Server is starting")
    os.system("java -jar -Xmx2048M -Xms2048M /home/pi/Desktop/testbot/server.jar
    await ctx.send("Server has started")

As you can see this is supposed to work on my raspberry pi. As soon as the server finishes starting, my bot goes offline. If i manually close the server, the bot comes back online and sends the "Server has started" message. How do i get my bot to stay online? Thanks very much in advance.

Edit:

I have now switched to the subprocess module.

@commands.command()
async def startmc(self, ctx):
    await ctx.send("Server is starting")
    subprocess.call("cd /home/pi/Desktop/testbot", shell = True)
    subprocess.call("java -jar -Xmx2048M -Xms2048M", shell = True)
    await ctx.send("Server has started")

My problem is still the same. Am i using the wrong command?

  • 1
    Does this answer your question? [Calling an external command from Python](https://stackoverflow.com/questions/89228/calling-an-external-command-from-python) – SomeRandomOwl Mar 02 '20 at 18:30
  • 1
    Does this answer your question? [How to start a background process in Python?](https://stackoverflow.com/questions/1196074/how-to-start-a-background-process-in-python) – Hymns For Disco Mar 02 '20 at 18:31
  • 3
    That java command will block until the process exits. – jordanm Mar 02 '20 at 18:31
  • @jordanm so there's no way my bot stays online while the server is also on? – Mouiadhofse Mar 02 '20 at 19:26
  • @Mouiadhofse see the question linked in my previous comment. It is possible to open a process "in the background" so it is non-blocking in your python program – Hymns For Disco Mar 03 '20 at 13:21
  • excuse me for asking again, i am very new to this, i have updated my function to use the subprocess module but it is still not working. I have a feeling that I should use something else than subprocess.call(). Is that right? – Mouiadhofse Mar 03 '20 at 14:33
  • @Mouiadhofse You are indeed correct. If you check the docs for [`subprocess.call`](https://docs.python.org/3/library/subprocess.html#subprocess.call), "Run the command described by args. Wait for command to complete ... ". This is why you are seeing that your "Server has started" message won't print until the server process is closed. Check the most voted [answer](https://stackoverflow.com/a/7224186/11424673) on the question I previously linked, it recommends to use `subprocess.Popen` – Hymns For Disco Mar 04 '20 at 05:16

1 Answers1

1

You are trying to open the java program but it blocks the bot from continuing to work so it shuts itself.

You need to call it in the background so it won't block the loop. You can do that using subprocess.Popen:

from subprocess import Popen
Popen("java -jar -Xmx2048M -Xms2048M /home/pi/Desktop/testbot/server.jar")
Guy Shefer
  • 86
  • 1
  • 6