0

I am try to create a .sh file to check to see if the server is running and if not to start it. i am not getting any echo's from this script or any fails but it is not running. can some one help me fix this or a better way to right it. i am not a programmer i just edited a few others scripts to get this.

JAVA="java -Xms512M -Xmx3072M -XX:PermSize=128m -jar FTBServer.jar nogui"
DIR="/home/jon/FTB/FTBnew"

if [ "$1" = start ] ; then
    if ps -ef | grep FTBServer.jar | grep -v -q grep ; then
        echo "Minecraft is already running"
    else
        echo "Starting!" && cd $DIR && screen -dmS minecraft $JAVA
        sleep 7
        if ps -ef | grep minecraft_server.jar | grep -v -q grep ; then
            echo "Minecraft server started successfully"
        else
            echo "Minecraft server failed to start"
        fi
    fi
fi

this is what i get and when i run it

root@FTB:/home/jon/FTB/FTBnew# bash Check.sh
root@FTB:/home/jon/FTB/FTBnew#
0

2 Answers 2

5

All you logic is inside an if [ "$1" = start ] block, so if you don't give that argument on the command line, your script doesn't do anything.

You should validate the arguments first:

if [ $# -ne 1 ] || [ "$1" != "start" ]; then
    echo "usage: $0 start"
    exit 1
else 
    # your stuff here
fi
1
  • Thanks for the comment after i removed the hole thing in general if [ "$1" = start ] it worked perfectly
    – smally2189
    Jan 21, 2014 at 16:53
0

You need to run it as

bash Check.sh start

... because nothing is done if [ "$1" != start ].

Also, this line

if ps -ef | grep minecraft_server.jar | grep -v -q grep ; then

should be

if ps -ef | grep FTBServer.jar | grep -v -q grep ; then

Some other notes:

  • Use arrays for Bash arguments.
  • Quote variables when they are used.
  • Use [[ instead of [.
  • if line correction as shown above.

So, a more correct version would be:

JAVA=(java -Xms512M -Xmx3072M -XX:PermSize=128m -jar FTBServer.jar nogui)
DIR="/home/jon/FTB/FTBnew"

if [[ "$1" == start ]]; then
    if ps -ef | grep FTBServer.jar | grep -v -q grep; then
        echo "Minecraft is already running"
    else
        echo "Starting!"
        cd "$DIR" &&
        screen -dmS minecraft "${JAVA[@]}"
        sleep 7
        if ps -ef | grep FTBServer.jar | grep -v -q grep; then
            echo "Minecraft server started successfully"
        else
            echo "Minecraft server failed to start"
        fi
    fi
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .