How do i run my server?

Started by
14 comments, last by ToniKorpela 12 years, 10 months ago
Hi.

I have finished my first multiplayer game. To host it i decided to buy a cloud server with CentOS. Before i started i had zero linux experience but i eventualy got everything I needed installed and configured properly and i got my game server to run nicely. I got a couple of frends to connect and play and it all works. The server is written in java and is contained in a single .jar file.

I can run the server on the command line using a SSH client (putty) but that prevents me from using the command line for anything else?. or (since i installed GNOME and free nx) i can remote dekstop in and run it from the terminal or i can just double click on the jar file (but that doesnt open a console! so i cant get any messages from it. i cant even see it running. I only know its running because i can connect to it).

None of these seem like the right way to be running a server. I can write a gui for the server and use the "double click on jar" option but i have a feeling i should be able to manage my server fully from a SSH client. On the other hand im going to use this server for all future multiplayer games i write so whatever solution i choose it needs to work for running multiple server applications.

I see linux use "services" for background applications. Should i make my server run as a "service" (how would i get output from it!) or is that not it either.

Im 100% new to servers and network programming (and im pretty sure my little server is going to go to hel once 50 people connects to it :D).

Any help appreciated. Thnx in Advance!
Advertisement

Hi.

I have finished my first multiplayer game. To host it i decided to buy a cloud server with CentOS. Before i started i had zero linux experience but i eventualy got everything I needed installed and configured properly and i got my game server to run nicely. I got a couple of frends to connect and play and it all works. The server is written in java and is contained in a single .jar file.

I can run the server on the command line using a SSH client (putty) but that prevents me from using the command line for anything else?. or (since i installed GNOME and free nx) i can remote dekstop in and run it from the terminal or i can just double click on the jar file (but that doesnt open a console! so i cant get any messages from it. i cant even see it running. I only know its running because i can connect to it).

None of these seem like the right way to be running a server. I can write a gui for the server and use the "double click on jar" option but i have a feeling i should be able to manage my server fully from a SSH client. On the other hand im going to use this server for all future multiplayer games i write so whatever solution i choose it needs to work for running multiple server applications.

I see linux use "services" for background applications. Should i make my server run as a "service" (how would i get output from it!) or is that not it either.

Im 100% new to servers and network programming (and im pretty sure my little server is going to go to hel once 50 people connects to it :D).

Any help appreciated. Thnx in Advance!


The way i normally deal with these things is through a program called GNU Screen (its installed by default in quite many Linux distributions and if it isn't you should be able to find a rpm or deb quite easily (for debian based distributions that don't include it by default it tends to be in the repositories)

To use it in its most simple form you just do:
> screen -S somenametoidentifythesession
(-S somename.. is optional but gives the screen session a name which is useful if you have multiple detatched screens for different purposes)
Screen opens and display a standard shell
> java yourseverapp
The server is now running and you can interact with it normally
pressing CTRL-a followed by d detatches the screen and gets you back to the normal console , you can reattach your screen by running screen -r somename... (if you havn't specified a name the session gets a name assigned to it automatically, if you just run screen -r it will list all currently running sessions by name (or attach it if its only one)
logging out won't kill the detached screen sessions, they'll keep running in the background until they are explicitly closed (a screen session is closed when you close all open windows in it (new windows can be opened with ctrl-a + c and are killed with ctrl-a + k , you can switch between windows in a screen session with ctrl-a + n and ctrl-a + p)

Full documentation for screen is here : http://www.gnu.org/s...ual/screen.html


You could also have the program write its output to a logfile or have a port open that you can connect to using for example telnet to send commands to the software.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Ah. that seems very usefull.

So just to confirm. Its safe to keep a screen session running indefenetley? say i had 3 servers running in a window each in 1 screen session for 8 months straight! (extreme example but still)

Ah. that seems very usefull.

So just to confirm. Its safe to keep a screen session running indefenetley? say i had 3 servers running in a window each in 1 screen session for 8 months straight! (extreme example but still)


screen itself is very stable and well tested, wether or not its suitable to have your server software running for 8 months straight however is another issue. (Your own software running in screen is likely to be more complex and less well tested and its also far more likely that you will have to apply updates to that software from time to time).

You might also need to apply kernel updates which "require" a full system reboot more frequently than once every 8 months. (While it is possible to patch a running linux kernel its generally not worth the hassle (Allthough there are obviously exceptions))
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
With 8 months i was specificaly refering to screen. As in asuming my server is perfect would it be able to run indefenetley in screen. It wasnt a real example :D. Il be very impressed with myself if my server runs stable for 24 hours! Thank you for the clear answers.
The traditional way to run a server on a POSIX system (like Linux) is through the init process. How the init process is configured depends on the distro and release, but it's likely init (and the sysv /etc/rc scripts) or upstart, or maybe the new systemd. The idea is that you write a shell script that starts you program and it daemonizes itself, but the LSB mandates a helper program to daemonize something like the running of a Java interpreter.

The advantage of the traditional initd startup over manually running your service from a remote CLI like screen is that the service will restart automatically on reboot. This is preferred for unattended systems.

That's not to say you can't log in remotely and start/stop/restart as required for testing. You could also make your server configurable so you can run a test server on a different port on the same machine.

You can test all of this out quite easily using a VM on your development machine.

Stephen M. Webb
Professional Free Software Developer

thnx Bregma. Il look into that aswel. For now i supose il stick to using screen for the sake of simplicity and becuase it solved my imediate problem.

Just one issue with screen. Everything works except killing windows! i can create screens and i can create windows in those screens and i can switch between windows in a screen and i can detach screens and reatach them again later. but i just cant seem to kill a window for some reason. (ctrl-a + k does nothing)

None of these seem like the right way to be running a server.


Agreed!

On Linux, you typically run things using either the newer upstart/services system, or the more mature init.d system. Or, if you're really old-school, the /etc/rc.local system :-)

There are a few things you need for a real service:

1) Any logging output from the server needs to be preserved in a file, and archived for a while, but eventually removed. Otherwise the hard disk would fill up.
Syslog if fantastic for this, because /var/log/messages gets rotated once a week, and purged after a few weeks, by default, on most Linux installations.
If your program just prints to stdout (or stderr) then you can turn it into a syslog-using program by piping the output to "logger -t yourname"
A typical server start command line might look something like:

(/path/to/java /path/to/mygame.jar </dev/null 2>&1 | logger -t mygame) &

What this does is tie the standard in of the game to /dev/null (so it doesn't die when you log off), tie standard error to standard out, and send standard out to the logger process so it goes to syslog. Finally, it runs all of that in the background, so the command line comes back immediately. You can see the process(es) running with "ps axf" or perhaps "top". At that point, you should probably also look into the "kill" command if you want to be able to stop it :-)

2) If the process is already running, you probably don't want to start a second copy. This is generally handled through a file in /var/run/ that you create and put your process ID in when the process starts, and that gets removed when you shut down the process. However, if the process crashes, that file may stay there, and need to be removed. A typical construct is to make the start script for your server do something like: * does /var/run/myname.pid exist? If so, complain and exit! * start the process * put the PID of the started process into /var/run/myname.pid
Note that there is a race condition there if you try to start the same process twice at the same time. There are fancier ways to solve this problem (see: upstart)

3) If the process crashes, you may want something to happen. Should it send you SMS? Should it log something? Should it auto-restart? If it auto-restarts, what if it crashes on start, and goes into an infinite restart loop? The specific requirements here are different for each system. Generally, you'll want to put a second script around this behavior that monitors and restarts the service, or whatever should happen.

4) Starting the service automatically when the server boots. If there's a link in /etc/rc2.d (or rc3.d) called S99myname, and it points at your init script (typically in /etc/init.d, called yourname) then that init script will be called with the argument "start" when the system boots. You can make arrangements for that script to start the server by putting the right code in that script. Traditionally, that script should also recognize a "stop" command (for ordered shutdown, removing lock files, etc) and "reload" (for re-loading configuration files while still running). For a more ghetto option, you can put a line into /etc/rc.local -- that file runs once, on system boot, and can start services that don't need other management.
enum Bool { True, False, FileNotFound };
You can also use a service like monit, which starts your server at boot time and restart it automatically if it crashes.
http://mmonit.com/monit/
Agree with everything hplus0603 said.

After you've run your server like this, if you want to watch the output in real time, you can use:
tail -f log_file_name

Also, not sure if this applies to Java or not, but the other thing you'll want to do is set your system up to generate core files in a specific directory, so when your server *does* crash, you can go and debug the core file with gdb (or whatever you do with Java).

This topic is closed to new replies.

Advertisement