LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Startup Script for Minecraft Server (https://www.linuxquestions.org/questions/linux-newbie-8/startup-script-for-minecraft-server-4175488666/)

Altiris 12-20-2013 06:48 AM

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.

schneidz 12-20-2013 08:09 AM

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.

nd7rmn8 12-20-2013 08:28 AM

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.

Altiris 12-20-2013 10:15 AM

Quote:

Originally Posted by schneidz (Post 5084489)
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 (Post 5084495)
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.

schneidz 12-20-2013 10:17 AM

Quote:

Originally Posted by Altiris (Post 5084563)
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 ?

Altiris 12-20-2013 02:14 PM

Quote:

Originally Posted by schneidz (Post 5084564)
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?

schneidz 12-20-2013 03:21 PM

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.

nd7rmn8 12-20-2013 03:32 PM

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

Altiris 12-20-2013 03:45 PM

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?

schneidz 12-20-2013 03:47 PM

Quote:

Originally Posted by nd7rmn8 (Post 5084717)
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.

schneidz 12-20-2013 03:53 PM

Quote:

Originally Posted by Altiris (Post 5084727)
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.

nd7rmn8 12-20-2013 04:32 PM

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.

Altiris 12-23-2013 06:02 PM

Quote:

Originally Posted by nd7rmn8 (Post 5084761)
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?

nd7rmn8 12-23-2013 07:22 PM

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.


All times are GMT -5. The time now is 01:37 AM.