Shell-Script

如何使用 pid 為 Fedora 上的 java 程序呼叫 .sh

  • March 26, 2018

我想啟動一個 Minecraft-Server。所以我下載了.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

startup-server.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
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

PID/home/minecraft/server/startup-server.sh腳本(新的 bash 程序)返回。你需要PID從這個命令中獲取/usr/bin/java -Xmx4096M -Xms4096M -jar minecraft_server.jar nogui

引用自:https://unix.stackexchange.com/questions/433676