在启动linux

时间:2016-04-24 15:14:19

标签: linux bash jar

我使用了标准的bash-startup脚本来自动启动我的Teamspeak服务器 - 它运行得很好。现在我已经为JTS3服务器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 startscript(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 Servers的模板。我只用我的Server Mod路径和脚本替换了一些东西并放入/etc/init.d/(chmod 755当然已经执行了)。

也许你们中的一些人有其他方法来解决这种奇怪的情况。非常感谢您的帮助!

祝你好运, Tw1x

1 个答案:

答案 0 :(得分:0)

我不知道问题是否在此期间得到解决,但在尝试同样的问题时。我发现脚本找不到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的开头确保"工作目录"是对的。

BTW - 我的初始化脚本: JTS-BOT

#!/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