How do i run my server?

Started by
14 comments, last by ToniKorpela 12 years, 10 months ago
Thanks for clearing everything up for me! I will set my server up the right way.
Advertisement
Myself I use a bash script to handle this type of stuff.

Here is an example:


#!/bin/sh
while true;
do
java -jar /path/to/jar.jar >> /path/to/log.txt;
done


Then i run the script in terminal:
$ sh run_server.sh &

This command will run the bash script and will hide the process so that you can still use the terminal. The script itself opens the server application and if the server crashes for some reason the while loop in the script will restart the server automatically. The > pipe will write the servers standard output into the file following specified after the pipe mark. Then you can even write function in the server to check if new build is available and the server shuts down and restarts automatically.
That script is great. I especially like the part where it overwrites the logs from a crash with the output of the server starting up again!

More seriously, consider using ">>" to append to the file, rather than replacing it. Or using one of the superior solutions listed above.
Damn, sorry wrote it on the fly. :D
[quote name='TMKCodes' timestamp='1308332203' post='4824536']


#!/bin/sh
while true;
do
java -jar /path/to/jar.jar >> /path/to/log.txt;
done



That script has two problems:

1) It doesn't capture standard error. Often, standard error is more important than standard out!
2) It doesn't disconnect the process from the controlling terminal. When you log out, it may kill the server.

However, 2) generally needs to happen in the shell that starts it daemonized, so the java part of your script might look something like:
java -jar /path/to/jar.jar >> /path/to/log.txt 2>&1 </dev/null
Your actual start command then needs to disconnect from the controlling TTY, too (or the restarter script will die):
nohup run-server.sh &

Note that your script still has a real problem: If something goes wrong with the start-up, the script does not detect this and stop trying to re-start. You'll just keep trying to re-start, spamming the log, without making any CPU headway, and without notifying anyone.
Btw: I don't like monit, because it's browser based for management, which doesn't scale to large systems, either. It's probably more suitable for a small system running for real than an infinite restart loop, though :-)
enum Bool { True, False, FileNotFound };
Thanks, didn't know about nohup. Anyway the logging can be done in many ways, but the topic specifically asks for how to start their server.

Your real problem might be real problem if you do not debug your code and make sure it starts every time, but it can be modified to notify if the server does not start. Though I have not realized this before as my programs usually do run at least for some time before crashing.

This topic is closed to new replies.

Advertisement