gpt4 book ai didi

linux - 在启动 linux 时运行 jar 文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:56 34 4
gpt4 key购买 nike

我使用了一个标准的 bash 启动脚本来自动启动我的 Teamspeak 服务器 - 效果很好。现在我对 JTS3 Server Mod 做了同样的事情,问题:它是一个 .jar 文件并且无法启动。如果我手动运行 .jar/startscript,它就可以正常工作。

这是/etc/init.d/中的脚本

#!/bin/bash
### BEGIN INIT INFO
# Provides: jts3servermod
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: JTS3 Server Mod
### END INIT INFO


# INIT Script by www.SysADMINsLife.com
######################################
# Customize values for your needs: "User"; "DIR"

USER="ts"
DIR="/home/ts/JTS3ServerMod"

###### Teamspeak 3 server start/stop script ######

case "$1" in
start)
su $USER -c "${DIR}/jts3servermod_startscript.sh start"
;;
stop)
su $USER -c "${DIR}/jts3servermod_startscript.sh stop"
;;
restart)
su $USER -c "${DIR}/jts3servermod_startscript.sh restart"
;;
status)
su $USER -c "${DIR}/jts3servermod_startscript.sh status"
;;
*)
echo "Usage: {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0

当然,它在 runlevel-folders 中与 update-rc.d jts3servermod defaults 链接。

servermod 启动脚本(jts3servermod_startscript.sh)是:

#!/bin/sh
# JTS3ServerMod Linux start script
# Created by Stefan "Stefan1200" Martens
# The author of this script is not responsible for any damage or data loss!

JAVA_COMMANDLINE_PARAMETERS="-mx30M" # You can add java arguments here, like the -mx30M argument!
JTS3SERVERMOD_COMMANDLINE_PARAMETERS="" # You can add JTS3ServerMod arguments here, like the -config and -log argument!
BINARYPATH="$(pwd)" # This have to point to the JTS3ServerMod directory!

# Don't change the lines below, if you are not a sh script expert!
cd "${BINARYPATH}"
BINARYNAME="JTS3ServerMod.jar"
ROOTUID="0"

case "$1" in
java)
if which java >/dev/null 2>&1 ; then
echo "Java is already installed:"
java -version
else
if [ "$(id -u)" -ne "$ROOTUID" ] ; then
echo "Start this script as root to start the automatic installation of the Java runtime environment."
echo "You can also read the system requirements of the JTS3ServerMod in the readme.txt file for a manual installation of the Java runtime environment."
exit 6
else
read -p "Do you wish to install the Java runtime environment? (y/n) " yn
case $yn in
[Yy]* ) installJava; break;;
* ) echo "Aborted!"; exit 6;;
esac
fi
fi
;;
start)
if ! which java >/dev/null 2>&1 ; then
echo "The JTS3ServerMod needs the Java runtime environment installed to run!"
echo "Start this script with the java argument as root to start the automatic installation of the Java runtime environment:"
echo "$0 java"
echo "You can also read the system requirements of the JTS3ServerMod in the readme.txt file for a manual installation of the Java runtime environment."
exit 6
fi
if [ "$(id -u)" -eq "$ROOTUID" ] ; then
echo "For security reasons it is prefered not to run the JTS3ServerMod as root!"
fi
if [ -e jts3servermod.pid ]; then
if ( kill -0 $(cat jts3servermod.pid) 2> /dev/null ); then
echo "The JTS3ServerMod is already running, try restart or stop!"
exit 1
else
echo "jts3servermod.pid found, but no JTS3ServerMod running. Possibly your previously started JTS3ServerMod crashed!"
echo "Please view the logfile for details."
rm -f jts3servermod.pid
fi
fi
echo "Starting the JTS3ServerMod..."
if [ -e "$BINARYNAME" ]; then
java ${JAVA_COMMANDLINE_PARAMETERS} -jar ${BINARYNAME} ${JTS3SERVERMOD_COMMANDLINE_PARAMETERS} > /dev/null &
PID=$!
ps -p ${PID} > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "JTS3ServerMod could not start!"
else
echo $PID > jts3servermod.pid
echo "JTS3ServerMod started, for details please view the log file!"
fi
else
echo "Could not find the file $BINARYNAME, aborting!"
exit 5
fi
;;
stop)
if [ -e jts3servermod.pid ]; then
echo -n "Stopping the JTS3ServerMod.."
if ( kill -TERM $(cat jts3servermod.pid) 2> /dev/null ); then
c=1
while [ "$c" -le 120 ]; do
if ( kill -0 $(cat jts3servermod.pid) 2> /dev/null ); then
echo -n "."
sleep 1
else
break
fi
c=$(($c+1))
done
fi
if ( kill -0 $(cat jts3servermod.pid) 2> /dev/null ); then
echo "JTS3ServerMod is not shutting down cleanly - killing!"
kill -KILL $(cat jts3servermod.pid)
else
echo "done"
fi
rm -f jts3servermod.pid
else
echo "No JTS3ServerMod running (jts3servermod.pid is missing)!"
exit 7
fi
;;
restart)
$0 stop && $0 start || exit 1
;;
status)
if [ -e jts3servermod.pid ]; then
if ( kill -0 $(cat jts3servermod.pid) 2> /dev/null ); then
echo "JTS3ServerMod is running!"
else
echo "JTS3ServerMod seems to have died!"
fi
else
echo "No JTS3ServerMod running (jts3servermod.pid is missing)!"
fi
;;
*)
echo "Usage: ${0} {start|stop|restart|status|java}"
exit 2
esac
exit 0

我还尝试编写一个自己的简单脚本,仅用于启动 jar 并捕获 pid 并将其写入文件。这些脚本都不想在启动期间启动我的 .jar 文件。

如您所见,第一个脚本是用于在启动期间启动 Teamspeak 服务器的模板。我只用我的 Server Mod 路径和脚本替换了一些东西并放入 /etc/init.d/(当然执行了 chmod 755)。

也许你们中的一些人有其他方法来解决这种奇怪的情况。

最佳答案

我不知道问题是否在此期间得到解决,但是在尝试相同的问题时。我发现脚本找不到 jar 文件。所以我补充说:

#!/bin/sh
# JTS3ServerMod Linux start script
# Created by Stefan "Stefan1200" Martens
# The author of this script is not responsible for any damage or data loss!
cd "$(dirname "$0")"
...

在jts3servermod_startscript.sh的开头确保“工作目录”是正确的。

顺便说一句——我的初始化脚本:jts-机器人

#!/bin/sh
### BEGIN INIT INFO
# Provides: jts-bot
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: JTS3-Bot for TeamSpeak
### END INIT INFO


USER="teamspeak"
DIR="/home/teamspeak/JTS3ServerMod"

###### JTS3ServerMod start/stop script ######

case "$1" in
start)
su $USER -c "${DIR}/jts3servermod_startscript.sh start"
;;
stop)
su $USER -c "${DIR}/jts3servermod_startscript.sh stop"
;;
restart)
su $USER -c "${DIR}/jts3servermod_startscript.sh restart"
;;
status)
su $USER -c "${DIR}/jts3servermod_startscript.sh status"
;;
*)
echo "Usage: -bash {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0

关于linux - 在启动 linux 时运行 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824970/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com