LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 07-10-2012, 08:01 AM   #1
cowboys1919
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
Question Rewriting a script for running a minecraft server on Ubuntu Server


I'm planning on running a web and minecraft server on ports 80 and 25565. The minecraft server has a special script, but i wrote it on the mac so it is a .command. I head that the mac's scripting is very similar to unix so this should be easy i hope (however I am 100% inexperienced with .sh scripting)

Basically, the java script will write to a file called "wasRestartOrStop.txt" a value of "0" if it was stopped normally, or "1" if it was a restart. I have a .command that looks like this (start_server.command).

Code:
#!/bin/bash
cd "$( dirname "$0" )"
java -Xmx1024M -Xms1024M -jar craftbukkit.jar
grep -1 '1' wasRestartOrStop.txt
if [ $? -eq 0 ]
then
	ditto GoodWorld world
	open start_server.command
	exit
else
        exit
fi
What it does is if it was a restart, copies the folder "GoodWorld" and overwrites the folder "world" with it, then starts the same command recursively. Seems pretty simple. I appreciate any help!

Last edited by cowboys1919; 07-10-2012 at 08:02 AM. Reason: forgot to mention something
 
Old 07-10-2012, 09:54 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,421
Blog Entries: 33

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Bash is bash. So that script will work the same on Linux. *.command and *.sh mean nothing. The line that matters is the first one in the script
Code:
#!/bin/bash
Which tells what shell it is supposed to run in.

The only thing you need to make sure is that the commands java and ditto are available on the system.

For the copying from one directory to another I would recommend replacing the ditto command with rsync.

Code:
#any added files in the target directory will be deleted
rsync -rptv --delete-before GoodWorld/ world/

#only update and don't remove files
rsync -rptv GoodWorld/ world/
The open command is still available on the Linux platform but why not run a while loop instead of opening a new shell every time?

Code:
#!/bin/bash
cd "$( dirname "$0" )"
while true;do
  java -Xmx1024M -Xms1024M -jar craftbukkit.jar
  exitcode=$?
  if [ ! "$exitcode" = "0" ];then
    echo "Error: Server exited with code $exitcode"
    break
  else
    echo -n "Do you want to restart? [y/N] (default N): "
    read ans
    if [ ! "$ans" = "y" ];then
      break
    fi
    #similar to the ditto command in that it will only copy files which differ.
    rsync -rptv GoodWorld/ world/
    echo "Restarting server..."
  fi
done
echo "Server has quit."
That same script will run on the Mac if bash is the interpretor and rsync command is available.

Last edited by sag47; 07-10-2012 at 10:00 AM.
 
1 members found this post helpful.
Old 07-10-2012, 10:34 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
I'm not sure what the "-1" (assuming it's a "one" and not "ell") option to grep is supposed to do. It doesn't appear to be an option in gnu grep, although it doesn't spit out an error for me. It appears to behave like the "-C 1" option in testing.

In any case, if the file always contains only a single "0" or "1", there's really no need for grep at all. You can instead simply read the contents directly into a variable and test that.

Code:
read status <wasRestartOrStop.txt

if (( status == 0 )); then
	...etc...
Notice that I also used ((..)) for the test. When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression

Also, dirname can be replaced with a simple parameter substitution.

Code:
cd "${0%/*}"

Last edited by David the H.; 07-10-2012 at 10:37 AM. Reason: formatting
 
Old 07-10-2012, 11:03 AM   #4
cowboys1919
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks, I'll be checking out these suggestions! I'll post whatever i come up with and if it doesn't work, well ask some more.
 
Old 07-10-2012, 11:31 AM   #5
cowboys1919
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Code:
#!/bin/bash
while true;do
  java -Xmx1024M -Xms1024M -jar craftbukkit.jar
  exitcode=$?
  if [ ! "$exitcode" = "0" ];then
    echo "Error: Server exited with code $exitcode"
    break
  else
    read status < wasRestartOrStop.txt
    if (( status == 0 )); then
      break
    else
      rsync -rptv --delete-before GoodWorld/ world/
      echo "Restarting server…(Will restart here in future)"
    fi
   fi
done
echo "Server has quit."
exit 0
I get unexpected end of file :/
Does this mean it's a syntax error?

Also, when i restart the server, what should i use, bash start_server.sh?
 
Old 07-10-2012, 12:17 PM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,421
Blog Entries: 33

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
Originally Posted by cowboys1919 View Post
Code:
#!/bin/bash
while true;do
  java -Xmx1024M -Xms1024M -jar craftbukkit.jar
  exitcode=$?
  if [ ! "$exitcode" = "0" ];then
    echo "Error: Server exited with code $exitcode"
    break
  else
    read status < wasRestartOrStop.txt
    if (( status == 0 )); then
      break
    else
      rsync -rptv --delete-before GoodWorld/ world/
      echo "Restarting server…(Will restart here in future)"
    fi
   fi
done
echo "Server has quit."
exit 0
I get unexpected end of file :/
Does this mean it's a syntax error?

Also, when i restart the server, what should i use, bash start_server.sh?
Assuming you're running this script in the foreground and not as a daemon. Ctrl+C or ^C is what you use to stop the java process which will then cycle through the restart prompts. Also I'm sure minecraft built in a "quit" function from the server commands. If this is meant to run in the background then a different approach must be taken.

Also following David's suggestion you should also use...
Code:
if (( $exitcode != 0 ));then ...
I'm a pretty oldschool script writer so it's hard to teach an old dog new tricks (I tend to forget about the double brackets). I also recommend that you globally exit the script with
Code:
exit $exitcode
Rather than "exit 0" like you have added since the java process appears to be what's important in this script.

As for your end of file error it's quite possible that the text file is in DOS format which sometimes messes with scripting. dos2unix command can resolve that or see if there's an option to avoid writing on non-*nix newlines (assuming that's even the problem).

Last edited by sag47; 07-10-2012 at 12:23 PM.
 
1 members found this post helpful.
Old 07-11-2012, 10:16 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
"unexpected end of file" errors are where the shell found the starting point of some syntax block, but didn't find the end point. It reached the end of the script first. Usually this means you forgot to include a quote mark or "fi" or something, but sometimes other errors confuse it, such as a line of unquoted (or improperly quoted) text that contains special characters of some kind (apostrophes are a common gotcha).

I don't see anything in your script offhand that would constitute such an error, however. There must be something else confusing it that's not immediately obvious. Does the error message give a line number? I also thought about the possibility of dos newlines, but considering that it was originally written on a mac, it seems unlikely.

(BTW, it would help in debugging if you formatted your script a bit more cleanly. Deeper indents and some blank lines between blocks would make it more readable.)


In any case, do you really need to check both the exit status and the contents of the file? My impression was that you only needed one or the other. Does the java app ever spit out any error codes other than 0 or 1, and is that what's stored in the text file, or is that something different?

You might also consider using a case statement to test the value, rather than an if/else structure.
 
1 members found this post helpful.
Old 07-11-2012, 10:36 AM   #8
cowboys1919
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Seems like the problem was the newlines or whatever and a dos2unix kind of command fixed it!
Thanks for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help me open a port for my Minecraft server Lumify Linux - Newbie 8 07-29-2012 01:52 PM
Minecraft Bukkit Server Console Window not showing up on cron in Ubuntu Chasingu Linux - Newbie 0 07-13-2011 08:29 PM
LXer: Minecraft Server on Ubuntu LXer Syndicated Linux News 0 06-11-2011 09:20 AM
[SOLVED] 1337 Minecraft Server enorbet Slackware 4 03-20-2011 05:45 PM
LXer: Running A File- And Print-Server With eBox On Ubuntu 8.04 Server LXer Syndicated Linux News 0 08-28-2008 06:20 PM


All times are GMT -5. The time now is 11:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration