Solved Restarting Server

Discussion in 'Plugin Development' started by Techtony96, Oct 24, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Techtony96

    How would I restart my Minecraft server? (Making a plugin that needs the server to be restarted)

    THANKS!
     
  2. Offline

    cman1885

    "Please restart the server for this to take effect"
    EDIT: why is this awaiting moderation
     
  3. Code:
    /reload
    
    Is that what you are asking?
     
  4. Offline

    gomeow

    Just reload, if its a config you want to reload, I think you use this:

    this.configReload();
     
  5. Offline

    Techtony96

    No, i want something that will shut down the server, and then start it back up.

    I need a complete server restart. But thanks, i can use that in the future

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  6. Offline

    gomeow

    I don't think this can be done with the starting back up part
     
  7. Use BukkitGUI it has a Restart Button
     
  8. Offline

    Courier

    There is not an easy way. I believe you would need to start another process before the server shuts down, and that process needs to pause until the server is shut down, then start the server in a new process. Most people use some sort of script to start up their server, so it would probably be best to load the name of their script from a config file.
     
  9. Offline

    Techtony96

    So do you think the best way to do this is to talk to my Host and ask them what script starts the server?
     
  10. Offline

    one4me

    There's probably a really simple solution, and in my attempt to go about restarting the server I put this together. Although I never did get it to work, maybe it could be of some use to someone else.
    Code:
    public void restart() {
      try {
        Class<?> c = Class.forName("java.lang.ApplicationShutdownHooks");
        Field f = c.getDeclaredField("hooks");
        f.setAccessible(true);
        List<Thread> list = new ArrayList<Thread>();
        Map<?, ?> hooks = (Map<?, ?>)f.get(null);
        for(Object thread : hooks.values()) {
          list.add((Thread)thread);
        }
        for(Thread thread : list) {
          Runtime.getRuntime().removeShutdownHook(thread);
        }
        Runtime.getRuntime().addShutdownHook(new Thread() {
          public void run() {
            try {
              RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
              List<String> arguments = RuntimemxBean.getInputArguments();
              List<String> arguments1 = new ArrayList<String>();
              arguments1.add("java");
              for(String o : arguments) {
                arguments1.add(" -" + o);
              }
              arguments1.add(" -jar " + Bukkit.class.getProtectionDomain().getCodeSource().getLocation().getPath());
              new ProcessBuilder(arguments1).start();
              }
            catch(Exception e) {
              e.printStackTrace();
            }
          }
        });
        Bukkit.shutdown();
      }
      catch(Exception e) {
        e.printStackTrace();
      }
    }
    
     
  11. Offline

    tom1000o

    have you tried messing with batch files? you run your server from a batch (usually), so why not make a batch that starts the restart batch? how you would go about doing it i wouldn't know, i've never done it before.
     
  12. Offline

    flyingtacoz

    I'm sure a good bat/sh restart file would work well also, does anyone have a good one that works?
     
  13. Offline

    md_5

    eeeeek. I would probably just close the server socket via reflection, spawn a new process running a user defined shell script, and then System.exit(0). Your method is pretty neat though, I think I might start using that, but why remove all shutdown hooks?
     
  14. Offline

    one4me

    Well I was trying to let CraftBukkit shutdown, update itself, and start back up without using a script. To prevent errors/crashes replacing the jar was suppose to be the last thing that happened, but since I couldn't specify my hook to run last I just removed all the others (laziness on my part).

    Also, unless someone adds another shutdown hook there should only be two hooks. I do admit it would probably be better to do a check to make sure you're only removing the hooks you want too - i.e. if(thread instanceof ServerShutdownThread), but since I also wanted the second hook removed as well (for debugging purposes) I didn't bother with the checks.

    I went ahead and messed around with the method again and I was able to restart the server by starting a batch file on shutdown, however that was something I was trying to avoid having to do. I was also able to launch the craftbukkit jar again, but unless I launched it by opening cmd first the console won't show.

    I did remove the part of code where it removed the shutdown hooks, as that wasn't really needed for the most part.
    Code:
    public void restart() {
      Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
          List<String> args = ManagementFactory.getRuntimeMXBean().getInputArguments();
          List<String> command = new ArrayList<String>();
          command.add(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java.exe");
          for(int i = 0; i < args.size(); i++) {
            command.add(args.get(i));
          }
          command.add("-jar");
          command.add(new File(Bukkit.class.getProtectionDomain().getCodeSource().getLocation().getFile()).getAbsolutePath());
          try {
            new ProcessBuilder(command).start();
          }
          catch(Exception e) {
            e.printStackTrace();
          }
        }
      });
      Bukkit.shutdown();
    } 
    
    If anyone knows how to make Java show the console whenever a jar is launched I'd really like to know how.

    And since some people have a server wrapper that auto restarts the server whenever it shuts down, you'd have to disable this so that more than one jvm don't end up running.
     
  15. tyry excuting the command "cmd /k <java> -jar <bukkit jar>
    for the stuff I enclosed in <> where parts that you already had
     
  16. Offline

    one4me

    I had gotten it to launch the window using the /c and /k flag while I was testing, but doesn't that only work for servers running on Windows? I may just have to check what the OS is and choose what program to launch it with based off of that.
     
  17. Offline

    Techtony96

    Another problem with these methods is that it only works for people hosting it on their home computer. Not if you pay for hosting.

    I have this problem solved, as my Host has an API for my Control Panel, but i will keep this thread alive for others that need to figure out how to do this. Maybe CraftBukkit will add this feature themselves later! :D
     
  18. Offline

    cfw34683

    did you ever get it to work. i wold really like to see your work
     
  19. Offline

    microgeek

    Add shutdown hook> Loop args> New process?
     
  20. Offline

    renaud444

    The BukkitGames plugin throws a [SEVER] to crash the server, then uses remoteToolKit to start it back up. I use McMyAdmin, so i just use the scheduler to make the server start when it crashes or stops. Multicraft does the same.
     
  21. I'm here to help you guys, this is what I use. It's doenst use code, but works fine if you host your self. I dont know about dedicated servers. But here you go:
    Code:
    @ECHO OFF
    SET BINDIR=%~dp0
    CD /D "%BINDIR%"
    :start
    "%ProgramFiles%\Java\jre7\bin\java.exe" -Xmx1024M -Xms1024M -jar craftbukkit.jar
    goto start
     
  22. Offline

    D3stinia

    I know this has already been marked as solved, but A very simple solution, is here if your hosting this on a windows OS and running from a .bat file.

    here is how mine operates. and this even works on crashes too.
    Code:
    @ECHO OFF
    :loop
    :: Maximum amount of Ram to use to run the server. Limited at 1582M for 32 Bit Java
    set MAXRAM32=1582M
    set MAXRAM64=10240M
    
    :: Don't edit past this point :: Don't edit past this point :: Don't edit past this point ::
    
    set JVM_VERSION=""
    if $SYSTEM_os_arch==x86 (
      set JVM_VERSION=32
    ) else (
      set JVM_VERSION=64
    )
    
    set TEMP_FILE=%TEMP%\javaCheck%RANDOM%%TIME:~9,5%.txt
    
    java -version 2>%TEMP_FILE%
    FOR /F "tokens=*" %%i in (%TEMP_FILE%) do (
      echo %%i | find "HotSpot" >nul
      if not errorlevel 1 (
        echo %%i | find "64-Bit" >nul
        if not errorlevel 1 (
          set JVM_VERSION=64
        ) else (
          set JVM_VERSION=32
        )
      )
    )
    
    del %TEMP_FILE%
    
    if %JVM_VERSION%==32 (
    echo Detected 32 Bit Java
    java -Xmx%MAXRAM32% -XX:MaxPermSize=256M -jar mcpc-plus-1.6.4-R2.1-forge965-B216.jar nogui
    ) else (
    echo Detected 64 Bit Java
    java -Xmx%MAXRAM64% -XX:MaxPermSize=256M -jar mcpc-plus-1.6.4-R2.1-forge965-B216.jar nogui
    )
    goto loop
    
     
Thread Status:
Not open for further replies.

Share This Page