Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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#
share|improve this question

2 Answers 2

up vote 5 down vote accepted

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
share|improve this answer
    
Thanks for the comment after i removed the hole thing in general if [ "$1" = start ] it worked perfectly –  smally2189 Jan 21 at 16:53

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.