我有一个在 EC2 实例 (AWS) 上运行的 Minecraft 服务器,该实例运行 Linux (CentOS)。我将minecraft.service
系统流程更改为
ExecStart=python3 run_server.py
此 python 脚本提取一个run.json
文件,其中填充了服务器 jar 名称、webhook URL、角色 ID 和 jvm 参数。它看起来是这样的:
{
"server_jar":"server.jar",
"jvm_args":"-Xms1G -Xmx4G",
"webhook_url":"your_url_here",
"role_id":"your_role_here"
}
当我sudo systemctl start minecraft
随后运行时,sudo systemctl status minecraft
我收到此错误:
with Popen(*popenargs, **kwargs) as p:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/lib/python3.12/subprocess.py", line 1953, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'java -Xmx3G -Xms3G -jar server.jar nogui'
答案1
虽然它有助于查看更多的 python 代码,但我有一种预感,您会收到此错误,因为它试图运行带有空格的大字符串作为单个命令名称。相反,您必须传递一个列表。
错误:
from subprocess import Popen, PIPE
process = Popen('java -Xmx3G -Xms3G -jar server.jar nogui', stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
正确的:
from subprocess import Popen, PIPE
process = Popen(
['java', '-Xmx3G', '-Xms3G', '-jar', 'server.jar', 'nogui'],
stdout=PIPE, stderr=PIPE
)
stdout, stderr = process.communicate()
看看它如何将长字符串分成不同的子部分并作为列表传递?
这个答案可以帮助更多:https://stackoverflow.com/questions/12605498/how-to-use-subprocess-popen-python