简体   繁体   中英

Raspberry Pi using a python script to launch minecraft server jar file

I have been researching how to run a minecraft server from the raspberry pi, so I finally got it set up!

So I thought to myself, I can go one set further... So I have been working on making a python script that runs the server whenever a button IRL is pressed. It worked very well until it said:

An exception occurred processing Appender File org.apache.logging.log4j.core.appender.AppenderLoggingException
Error writing to RandomAccessFile logs/latest.log

my code is as follows:

import RPi.GPIO as GPIO
import os
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

try:
while True:
    #Test to see if the button has been pressed
    if (GPIO.input(11) == 1):
        print ("Downloading Server to clear corruption...")
        #download specified version
        os.system("wget https://s3.amazonaws.com/Minecraft.Download/versions/1.8.1/minecraft_server.1.8.1.jar")
        print ("Moving file...")
        os.system("mv /home/pi/minecraft_server.1.8.1.jar /home/pi/Server/server.jar")
        print ("Finished")
        print ("Starting Server")
        #start server
        os.system("java -Xms1024M -Xms1024M -jar /home/pi/Server/server.jar nogui")
        print ("Server Stopped!")
        time.sleep(2)

except KeyboardInterrupt:
    GPIO.cleanup()

When I run the server as

cd Server
java -Xms1024M -Xms1024M -jar server.jar nogui

It works perfectly!

I think the problem is that when I run the python script, it does not have enough authority so it cannot edit files.

I launch the script as

sudo python controller.py

My question is; is there anyway on making a python script run a terminal command with the power of superuser?

为了回答问题底部的问题,您可以通过在命令本身前面加上sudo来使Python脚本运行终端命令。

 os.system("sudo java -Xms1024M -Xms1024M -jar /home/pi/Server/server.jar nogui")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM