LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-20-2013, 06:48 AM   #1
Altiris
Member
 
Registered: Mar 2013
Posts: 258

Rep: Reputation: Disabled
Startup Script for Minecraft Server


Alright so I have a Minecraft server running for a couple of friends and I. Right now every time my CentOS box restarts, which isnt a lot, (the computer the server runs off of) I need to manually run the .sh script that starts the server. I want this make this process automatic, however I do not want to make it a service and I also want to be able to see the server console whenever I want to.

What I want-
I would like to create a .sh script to run on startup which would make a terminal open when logged in, then execute another .sh script (this other .sh script is the script I currently use the start the server manually)
How would I do this? Thanks.
 
Old 12-20-2013, 08:09 AM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,917

Rep: Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599
you can put commands in /etc/rc.local and they will run when the system boots up.

or you can put stuff in ~/.profile or ~/.bash_profile and they will run when that user logs in.

or you can put an @reboot entry in your crontab and the command will run after every reboot.

Last edited by schneidz; 12-20-2013 at 10:18 AM.
 
Old 12-20-2013, 08:28 AM   #3
nd7rmn8
Member
 
Registered: Jan 2013
Distribution: Arch
Posts: 50

Rep: Reputation: 11
For the portion of your question concerning being able to see the console whenever you want to, screen would be a great program to look into for this. I keep meaning to learn how to do more with it.

Quote:
What is GNU Screen? it is a terminal multiplexer and you can run multiple console-based applications simultaneously. The best part of it is that you can leave it running on remote machines and come back to pick up your console sessions.
http://www.phacks.net/how-to-use-gnu-screen/

Here is an example of using it to start the program top, name the session foo, and immediately detach the screen.

Code:
screen -d -m -S foo top

-d -m    starts screen in detached mode
-S foo   names the session foo
top      name of program running
to reattach to the screen session.
Code:
screen -r foo
to detach it again, ctrl A-D.

*edit* added this:
A less powerful, but simpler way, would be to background the process and redirect output to a file, you could view output by viewing the file, but you would be unable to interact with the console. In this particular situation, it may be better, because there is no need to interact with the minecraft script after its started, but its a good excuse to learn screen.

Last edited by nd7rmn8; 12-20-2013 at 08:34 AM.
 
Old 12-20-2013, 10:15 AM   #4
Altiris
Member
 
Registered: Mar 2013
Posts: 258

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
you can put commands in /etc/rc.local and they will run when the system boots up.

or you can put stuff in ~/.profile or ~.bash_profile and they will run when that user logs in.

or you can put an @reboot entry in your crontab and the command will run after every reboot.
So I did not have a .profile folder so I made my own, I assume now I just drop in a .sh file to tell it to execute the other sh script? Is that correct?

Quote:
Originally Posted by nd7rmn8 View Post
For the portion of your question concerning being able to see the console whenever you want to, screen would be a great program to look into for this. I keep meaning to learn how to do more with it.


http://www.phacks.net/how-to-use-gnu-screen/

Here is an example of using it to start the program top, name the session foo, and immediately detach the screen.

Code:
screen -d -m -S foo top

-d -m    starts screen in detached mode
-S foo   names the session foo
top      name of program running
to reattach to the screen session.
Code:
screen -r foo
to detach it again, ctrl A-D.

*edit* added this:
A less powerful, but simpler way, would be to background the process and redirect output to a file, you could view output by viewing the file, but you would be unable to interact with the console. In this particular situation, it may be better, because there is no need to interact with the minecraft script after its started, but its a good excuse to learn screen.
That sounds really cool, at the moment I do not have too much time to experiment with it though, maybe in the future.
 
Old 12-20-2013, 10:17 AM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,917

Rep: Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599
Quote:
Originally Posted by Altiris View Post
So I did not have a .profile folder so I made my own, I assume now I just drop in a .sh file to tell it to execute the other sh script? Is that correct?
...
nope... does the user have a .bash_profile file ?
 
Old 12-20-2013, 02:14 PM   #6
Altiris
Member
 
Registered: Mar 2013
Posts: 258

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
nope... does the user have a .bash_profile file ?
Yes .bash_profile I have. I do not know if I specified but I am using CentOS 6.5 (sorry if I didnt). I just realized that what I asked to do above would still prompt me to not be able to access the console. I want to make a .sh file and inside that .sh file have commands that open the terminal and then paste in code into the terminal (if possible). Is the way I am trying to do it hacky/not recommended? Should I find some time to work with screen?
 
Old 12-20-2013, 03:21 PM   #7
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,917

Rep: Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599
not really sure what you are trying to do (or why ?) but if all you want to do is have a terminal started when you login try putting this in the last line of .bash_profile:
Code:
/usr/bin/gnome-terminal &
and then you can manually copy-paste whatever commands you need into that window.
 
Old 12-20-2013, 03:32 PM   #8
nd7rmn8
Member
 
Registered: Jan 2013
Distribution: Arch
Posts: 50

Rep: Reputation: 11
not sure what the syntax is for gnome-terminal, but for xterm, if you wanted to open up a new terminal window and run a command in it, lets use top for this example, it goes like this.

Code:
xterm -e top &
so just replace top with the startup script for minecraft
 
1 members found this post helpful.
Old 12-20-2013, 03:45 PM   #9
Altiris
Member
 
Registered: Mar 2013
Posts: 258

Original Poster
Rep: Reputation: Disabled
Sorry for not being clear. To start my server, I usually launch up a terminal then type in a java command which starts the server (I also sometimes launch a terminal, cd to the directory where my .sh script and then in the terminal run the .sh script). I am basically trying to do this but automatically, would screen be a better option?
 
Old 12-20-2013, 03:47 PM   #10
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,917

Rep: Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599
Quote:
Originally Posted by nd7rmn8 View Post
not sure what the syntax is for gnome-terminal, but for xterm, if you wanted to open up a new terminal window and run a command in it, lets use top for this example, it goes like this.

Code:
xterm -e top &
so just replace top with the startup script for minecraft
Code:
/usr/bin/gnome-terminal -e top &
i wasnt sure from the op's description if that is what they wanted.
 
Old 12-20-2013, 03:53 PM   #11
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 3,917

Rep: Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599Reputation: 599
Quote:
Originally Posted by Altiris View Post
Sorry for not being clear. To start my server, I usually launch up a terminal then type in a java command which starts the server (I also sometimes launch a terminal, cd to the directory where my .sh script and then in the terminal run the .sh script). I am basically trying to do this but automatically, would screen be a better option?
heres an example of what i do to automatically start things on my pc:
Code:
[schneidz@hyper ~]$ cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

# rm /etc/X11/xorg.conf

# ifconfig eth3 down && ifconfig eth3 up

#/home/schneidz/nfs-w/leaderboard.ksh &
note: it is commented out because it no longer runs but you get the idea.
 
Old 12-20-2013, 04:32 PM   #12
nd7rmn8
Member
 
Registered: Jan 2013
Distribution: Arch
Posts: 50

Rep: Reputation: 11
screen could be overkill for this particular situation. I know I suggested it earlier, and it is useful, but since there is no need to interact with the script after its started... here is a simpler minecraft server startup script for an example. It is actually what I use on mine.

Code:
java -Xmx1024M -Xms1024M -jar /path/to/the/minecraft_server.1.7.4.jar nogui >> /path/to/the/mc.log 2>&1 &
the "-Xmx1024M -Xms1024M" tell java how much ram to commit to this server. 1GB in my case.
the "&" at the end backgrounds the process
the ">> /path/to/the/mc.log 2>&1" sends all output to a file (/path/to/the/mc.log)

After this is started, you can view the output of the server by either of these 2 following ways.
Code:
cat /path/to/the/mc.log
   or
tail -f /path/to/the/mc.log
"cat" just displays everything that is in the file.
"tail -f" will update the display in the terminal window whenever new text is added.

With this script, I would just start it on boot by placinig it in rc.local or whatever your distro uses. Then, anytime you want, you can monitor the output of the server by typing one of the above commands in a terminal.
 
1 members found this post helpful.
Old 12-23-2013, 06:02 PM   #13
Altiris
Member
 
Registered: Mar 2013
Posts: 258

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by nd7rmn8 View Post
screen could be overkill for this particular situation. I know I suggested it earlier, and it is useful, but since there is no need to interact with the script after its started... here is a simpler minecraft server startup script for an example. It is actually what I use on mine.

Code:
java -Xmx1024M -Xms1024M -jar /path/to/the/minecraft_server.1.7.4.jar nogui >> /path/to/the/mc.log 2>&1 &
the "-Xmx1024M -Xms1024M" tell java how much ram to commit to this server. 1GB in my case.
the "&" at the end backgrounds the process
the ">> /path/to/the/mc.log 2>&1" sends all output to a file (/path/to/the/mc.log)

After this is started, you can view the output of the server by either of these 2 following ways.
Code:
cat /path/to/the/mc.log
   or
tail -f /path/to/the/mc.log
"cat" just displays everything that is in the file.
"tail -f" will update the display in the terminal window whenever new text is added.

With this script, I would just start it on boot by placinig it in rc.local or whatever your distro uses. Then, anytime you want, you can monitor the output of the server by typing one of the above commands in a terminal.
This seems to be a pretty good method, thanks, also, because the computer isnt constantly outtputting the text into terminal, does this save CPU usage or no because it needs to still get written to file?! I am starting to get interested in screen though, but im not actually sure what it is. Is it something where I just install the package and keeps SSH sessions connected even while exiting the client? I have seen some Garry's Mod Server tutorials recommend to get screen but I never saw the point of it. I still dont see the point of ME personally using it as my servers dont really run 24/7 (except for Minecraft) although I know it is a somewhat helpful tool.

Question: how would I properly turn off the server, I dont want to shut it off with some hacky method. Should I learn how to use screen?

Last edited by Altiris; 12-23-2013 at 06:23 PM.
 
Old 12-23-2013, 07:22 PM   #14
nd7rmn8
Member
 
Registered: Jan 2013
Distribution: Arch
Posts: 50

Rep: Reputation: 11
it saves some cpu usage and ram, mostly just because you don't have to have a terminal window open. More importantly you dont have to worry about accidentally closing out the terminal and crashing the server.

The best way to shutdown a minecraft server is to issue the commands /save-all and then /stop from within the game. You need to have administrator privileges though. In order to gain administrator priviliges, add your name to the ops.txt file in the games directory and restart. Here are the 2 relevant web pages.
http://minecraft.gamepedia.com/Administrator
http://minecraft.gamepedia.com/Comma...-only_Commands

To force a process to quit, you can find the process's pid and then kill it using the kill command. But, this doesn't kill programs gracefully, it may not save all changes before it exits. Here is an example of what it would look like on my server if I was to shut down minecraft this way. I use the grep command to filter out any process that doesn't have java in the name. If the kill command doesn't work, you may have to add sudo in front of it or use "kill -9" in order force it to stop.

Code:
server$ ps -aux | grep java
nd7rmn8   1579  5.1 59.4 2428420 1222400 ?     Sl   Dec10 972:39 java -Xmx1024M -Xms1024M -jar minecraft_server.1.7.4.jar nogui
server$ kill 1579
screens main benefit in my opinion, is having multiple terminals available in one. Also the ability to open up that screen in another terminal at a later date and from another computer or session. Not necessary for minecraft servers as the server's console is not interactive (as far as I know), but say the server's console was interactive and you needed to log in remotely and type in a command inside that console, screen would allow you to do that.

im sure i messed up some of the jargon with that, hope you get the point though.
 
  


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
Minecraft world trickery (and even minecraft on ipad) lhartvik LinuxQuestions.org Member Success Stories 1 01-09-2014 06:03 PM
LXer: GOL MineCraft Weekly. Grab Issue 2 Today, Linux Minecraft server LXer Syndicated Linux News 0 08-04-2013 11:41 AM
Create a script to run Minecraft when a player connects JoseCuervo Linux - Games 3 02-27-2013 11:55 PM
[SOLVED] Rewriting a script for running a minecraft server on Ubuntu Server cowboys1919 Linux - Server 7 07-11-2012 10:36 AM
server startup script elainelaw Linux - Newbie 6 02-18-2010 01:11 AM


All times are GMT -5. The time now is 05:15 AM.

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