我想启动一个 Minecraft 服务器。所以我下载了.jar
文件,安装了java 8,创建了用户和目录,如下所示:
ls -la
insgesamt 29552
drwxrwxr-x. 4 minecraft minecraft 235 26. Mär 19:32 .
drwx------. 3 minecraft minecraft 97 25. Mär 19:28 ..
-rw-rw-r--. 1 minecraft minecraft 2 26. Mär 21:16 banned-ips.json
-rw-rw-r--. 1 minecraft minecraft 2 26. Mär 21:16 banned-players.json
-rw-rw-r--. 1 minecraft minecraft 181 25. Mär 19:22 eula.txt
drwxrwxr-x. 2 minecraft minecraft 4096 26. Mär 21:16 logs
-rwxrwxr-x. 1 minecraft minecraft 30222121 18. Sep 2017 minecraft_server.jar
-rw-rw-r--. 1 minecraft minecraft 2 26. Mär 21:16 ops.json
-rw-rw-r--. 1 minecraft minecraft 754 26. Mär 21:16 server.properties
-rwxrwxr-x. 1 minecraft minecraft 141 26. Mär 21:08 startup-server.sh
-rw-rw-r--. 1 minecraft minecraft 214 26. Mär 21:17 usercache.json
-rw-rw-r--. 1 minecraft minecraft 2 25. Mär 19:23 whitelist.json
drwxrwxr-x. 9 minecraft minecraft 166 26. Mär 21:38 world
启动服务器.sh:
#!/bin/bash
echo I am starting server now
now=$(date +%d_%m_%Y_%H_%M_%S)
/usr/bin/java -Xmx4096M -Xms4096M -jar minecraft_server.jar nogui
/usr/bin/startup-minecraft-server:
#!/bin/sh
SERVICE_NAME=Minecraft-Server
PID_PATH_NAME=/tmp/minecraft-server.pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
exec /home/minecraft/server/startup-server.sh > /dev/null 2>&1 & echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
$0 stop
$0 start
else
echo "$SERVICE_NAME is not running ..."
fi
;;
status)
if [ -e $PID_PATH_NAME ]; then
echo minecraft server is running, pid=`cat $PID_PATH_NAME`
else
echo minecraft server is NOT running
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
每当我打电话
startup-minecraft-server start
它告诉我:
./startup-minecraft-server start
Starting Minecraft-Server ...
Minecraft-Server started ...
并检查状态:
./startup-minecraft-server status
minecraft server is running, pid=30639
但
ps -p 30639 -o comm=
结果一无所获。我不知道为什么会发生这种情况。
编辑:
我修改了脚本,但仍然得到与以前相同的结果:
#!/bin/sh
SERVICE_NAME=Minecraft-Server
PATH_TO_JAR=/home/minecraft/server/minecraft_server.jar
PID_PATH_NAME=/tmp/minecraft-server.pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
exec /usr/bin/java -Xmx4096M -Xms4096M -jar $PATH_TO_JAR nogui > /dev/null 2>&1 & echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
$0 stop
$0 start
else
echo "$SERVICE_NAME is not running ..."
fi
;;
status)
if [ -e $PID_PATH_NAME ]; then
echo minecraft server is running, pid=`cat $PID_PATH_NAME`
else
echo minecraft server is NOT running
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
答案1
exec /home/minecraft/server/startup-server.sh > /dev/null 2>&1 & echo $!
startup-server.sh
给你几乎立即退出的PID 。但你想要的PID是
/usr/bin/java -Xmx4096M -Xms4096M -jar minecraft_server.jar nogui
对于您的工作方式,您需要
exec /usr/bin/java -Xmx4096M -Xms4096M -jar minecraft_server.jar nogui
另外还if [ -f $PID_PATH_NAME ]
不够。您应该始终检查该进程是否仍然存在并且是正确的。
使用类似的东西可能是有意义的startproc
。
答案2
这是PID
从/home/minecraft/server/startup-server.sh
脚本返回的(新的 bash 进程)。你需要PID
从这个命令中获取/usr/bin/java -Xmx4096M -Xms4096M -jar minecraft_server.jar nogui
。
答案3
这行修复了它:
删除守护进程并添加 > 到 echo $!
/usr/bin/java -Xmx4096M -Xms4096M -jar $PATH_TO_JAR nogui > /dev/null 2>&1 & echo $! >>$PID_PATH_NAME
我也做了:
cd /home/minecraft/server/
pwd
在脚本的开头