简体   繁体   English

Raspberry Pi使用python脚本启动minecraft服务器jar文件

[英]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! 我一直在研究如何从树莓派运行Minecraft服务器,因此终于完成了设置!

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. 所以我心想,我可以再做一组...因此,我一直在努力制作一个每当按下IRL按钮时都会运行服务器的python脚本。 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. 我认为问题在于,当我运行python脚本时,它没有足够的权限,因此无法编辑文件。

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? 无论如何,要使python脚本以超级用户的能力运行终端命令吗?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用在Raspberry Pi上后台运行的python脚本创建文本文件 - Creating a text file using a python script that runs in background on Raspberry Pi Crontab 在树莓派上启动 python 脚本导致导入错误 - Crontab to launch python script on raspberry pi causes import error 在Raspberry Pi Web服务器中从Web运行python脚本 - Run python script from the web in a Raspberry Pi web server 自动运行python脚本将输出保存到txt文件raspberry pi - Autorun python script save output to txt file raspberry pi 使用AJAX执行拍摄照片的python脚本(raspberry pi) - Using AJAX to execute a python script that takes a picture (raspberry pi) 有没有办法在树莓派中使用 python 脚本获取 roscore 字符串? - Is there any way to get roscore string using python script in raspberry pi? 在Raspberry Pi上使用Nginx从HTML页面运行Python脚本 - Run a Python script from a HTML page using nginx on a Raspberry Pi Raspberry Pi 4B,启动时使用串口运行 Python 脚本 - Raspberry Pi 4B, running Python Script using serial at boot 如何将日志记录添加到带有时间戳的文件到Raspberry Pi的Python TCP服务器 - How to add logging to a file with timestamps to a Python TCP Server for Raspberry Pi Python 在树莓派和文件服务器之间复制图片 Windows - Python Copy a Picture between a Raspberry Pi and a Windows File server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM