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
add comment

4 Answers

up vote -1 down vote accepted

Give execute permission to that script by,

sudo chmod +x Check.sh

Then run that file by,

./Check.sh
share|improve this answer
4  
That actually doesn't solve the problem but is just a more fancy way of doing bash Check.sh. –  Florian Diesch Jan 21 at 16:53
1  
i tried it anyways but it didn't work –  smally2189 Jan 21 at 16:54
    
Eventhough op changes the script,he needs to change the file as executable otherwise it won't work. –  Avinash Raj Jan 21 at 16:54
1  
It would throw a big error if the script isn't executable. –  Braiam Jan 22 at 1:43
add comment

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
add comment

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
add comment

You should add shebang line at top line

 #!/bin/bash
share|improve this answer
    
i do i just didn't add the commented out parts –  smally2189 Jan 21 at 16:54
    
Maybe it requires root privilege –  kenn Jan 21 at 16:58
add comment

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.