Shutting Down Server

Das nächste Update wird am Mittwoch, dem 26. September 2018 gegen Abend verfügbar sein. Sorry für die große Verzögerung!

  • Shutting Down Server

    Hi, I would like to create an automated/controlled server shutdown. Now, firstly, is this already possible? In which case show me the way to Tipperary!

    What I would like to do is something similar to this:

    Step 1, global announcement to players that server is rebooting soon (easily done)
    Step 2, stop any new connections to the server (not sure if this is possible? maybe set max players to 0?)
    Step 3, after x period of time, kick any remaining players that are online (so that there are zero players online)
    Step 4, restart server script (not sure how to automate via java? (which is preferable)
    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • yahwho schrieb:

    Now, firstly, is this already possible? In which case show me the way to Tipperary!
    Through the API you can shutdown the server but you cannot restart it after atm

    yahwho schrieb:

    Step 2, stop any new connections to the server (not sure if this is possible? maybe set max players to 0?)
    Well you can use the .setCancelled(true) method in the PlayerConnectEvent for this ;)

    yahwho schrieb:

    Step 4, restart server script (not sure how to automate via java? (which is preferable)
    There are some restart scripts on the forum but none for java, server restarts are moslty done via shell commands, not sure if SteamCMD has any options like that too :/
    Admin on Artisan's Realm
    Rising World Projects:
    Blueprint Texture Editor
    MailingSystem
    ServerTools
    Portals
  • yahwho schrieb:

    I can always write a script starting the server, then call that script from Java.
    I know python has a subprocess module (docs.python.org/3/library/subprocess.html) in which you specify what code and in which language to execute it at that works really well for shell commands. Probably java has something similar but I haven't personally looked into it.
    Admin on Artisan's Realm
    Rising World Projects:
    Blueprint Texture Editor
    MailingSystem
    ServerTools
    Portals
  • Java-Quellcode

    1. import java.io.*;
    2. public class CmdTest {
    3. public static void main(String[] args) throws Exception {
    4. ProcessBuilder builder = new ProcessBuilder(
    5. "cmd.exe", "/c", "cd \"C:\\Program Files\\Microsoft SQL Server\" && dir");
    6. builder.redirectErrorStream(true);
    7. Process p = builder.start();
    8. BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    9. String line;
    10. while (true) {
    11. line = r.readLine();
    12. if (line == null) { break; }
    13. System.out.println(line);
    14. }
    15. }
    16. }
    Alles anzeigen
    That's how I'm going to run a script from Java. 8)
    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • The only issue when spawning a new process from within the JVM is that it's treated as a child process. This means if the JVM is closed, all child processes will be killed as well :/ You can add shutdown hooks to the JVM though (and start a new process there), but they're still affected by the death of the JVM (it kinda works on Windows but becomes very awkward on Linux)...
  • red51 schrieb:

    The only issue when spawning a new process from within the JVM is that it's treated as a child process. This means if the JVM is closed, all child processes will be killed as well :/ You can add shutdown hooks to the JVM though (and start a new process there), but they're still affected by the death of the JVM (it kinda works on Windows but becomes very awkward on Linux)...
    Thanks for that. I'm by no means knowledgeable about the real ins and outs of java. Still learning! :D

    So.

    I guess the easiest way around this is to schedule (via a plugin) a "clean shutdown" at say midnight.

    Then at, say 2 minutes past midnight set a Scheduled Task to run the server.

    :thumbup:
    Medieval Realms - connect to server via server.medievalrealms.co.uk
    Medieval Realms Website
  • yahwho schrieb:

    I guess the easiest way around this is to schedule (via a plugin) a "clean shutdown" at say midnight.
    Yes, that would work. You can either create a plugin which sends the messages and shuts the server down, or alternatively you can just send input commands to the server process (e.g. "shutdown" to shutdown the server, or "say" or "yell" to send a message to all players). In case you create a script to handle the restart, the input commands may be handier (so there is no need to use the plugin API at all) :)