Restart Command

Discussion in 'Plugin Development' started by BungeeTheCookie, Oct 26, 2013.

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

    BungeeTheCookie

    Hello people of Bukkit! I was wondering how you could restart the server. What I mean by restart is having the server shut down, and right before it exits everything, it starts back up again. Multicraft has a way of doing this, and I have seen this in a few plugins, and the Spigot Project by md_5, and I was wondering how you can do this by command. I know this is possible because I have seen only a few methods of doing this, but it works. Like, kind of like an Auto-Restart, but without the Bukkit Scheduler System. Any and ALL HELP is appreciated. Thanks! :)
     
  2. BungeeTheCookie
    This requires some manual work from the server manager, but it should be as simple as
    • Stopping the server with
    Code:java
    1. Bukkit.getServer().shutdown();

    • then editing your startup batch file
    Code:java
    1. ECHO OFF
    2. SET BINDIR=%~dp0
    3. CD /D "%BINDIR%"
    4. :start
    5. java -Xms1024M -Xmx1024M -jar craftbukkit.jar
    6. goto start

    Obviously edit the min and max ram you want to use.
    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.command.Command;
    3. import org.bukkit.command.CommandSender;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class SomeClass extends JavaPlugin implements Listener {
    8.  
    9. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    10. if(cmd.getName().equalsIgnoreCase("restart")) {
    11. Bukkit.getServer().shutdown();
    12. }
    13.  
    14. return false;
    15. }
    16. }
     
  3. Offline

    BungeeTheCookie

    Assist
    Is there anyway to do this without editing your start batch file?
    If not, is there a way to get the batch file, and editing it and then saving it?
    And what about with dedicated minecraft server hosts?

    Assist
    Like, accessing the batch file from either a dedicated server, or the run.bat and adding that line of code to it? Or is it impossible?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. BungeeTheCookie
    It is possible to write something in the batch file, I'm just struggling to figure out how to write on specific lines. This is what I've tried, but it writes the lines to the bottom of the file instead of the ones I want.
    Code:java
    1. package test;
    2.  
    3. import java.io.BufferedWriter;
    4. import java.io.File;
    5. import java.io.FileNotFoundException;
    6. import java.io.FileReader;
    7. import java.io.FileWriter;
    8. import java.io.IOException;
    9. import java.io.LineNumberReader;
    10.  
    11. public class ModifyBatch {
    12.  
    13. public static void main(String[] args) {
    14. File file = new File("C:" + File.separator + "data", "data.yml");
    15. modifyBatch(file.getPath());
    16. }
    17.  
    18. public static void modifyBatch(String path) {
    19. File file = new File(path);
    20. BufferedWriter bufferedWriter = null;
    21. LineNumberReader lineNumberReader = null;
    22.  
    23. try {
    24. bufferedWriter = new BufferedWriter(new FileWriter(file, true));
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. }
    28.  
    29. try {
    30. lineNumberReader = new LineNumberReader(new FileReader(file));
    31. } catch (FileNotFoundException e) {
    32. e.printStackTrace();
    33. }
    34.  
    35. System.out.println("WRITING...");
    36.  
    37. lineNumberReader.setLineNumber(3);
    38.  
    39. try {
    40. bufferedWriter.newLine();
    41. bufferedWriter.write(":start");
    42. System.out.println("WROTE A LINE - LINENUMBER " + lineNumberReader.getLineNumber());
    43. } catch(IOException ex) {
    44. ex.printStackTrace();
    45. }
    46.  
    47. lineNumberReader.setLineNumber(5);
    48.  
    49. try {
    50. bufferedWriter.newLine();
    51. bufferedWriter.write("goto start");
    52. System.out.println("WROTE A LINE - LINENUMBER " + lineNumberReader.getLineNumber());
    53. } catch(IOException ex) {
    54. ex.printStackTrace();
    55. }
    56.  
    57. try {
    58. bufferedWriter.close();
    59. lineNumberReader.close();
    60. } catch (IOException e) {
    61. e.printStackTrace();
    62. }
    63.  
    64. System.out.println("FINISHED WRITING");
    65. }
    66. }
    67.  


    BungeeTheCookie
    I hate to double post, but editing my previous post would mess up the format. An option would be to save the contents of the batch file to a temporary text file, then clear the batch file, and start writing a new one using the old contents and the new files. I'll write something, just a sec.

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

    BungeeTheCookie

    Assist
    Ok thanks! I really appreciate you trying to help me out. :D
    I'll get back to you when you post the new code :D
     
  6. Offline

    1Rogue


    Couple things:

    -Xms isn't the minimum ram, it sets the initial heap size on startup.

    -Why are you using a goto, rather than some form of scheduling with a task or executing a different script?
     
  7. Offline

    The_Doctor_123

    You could possibly spin off a new process when you type the restart command? Not so sure how well that would work.
     
  8. Offline

    BungeeTheCookie

  9. Offline

    1Rogue

    Are you using linux or windows? (I'm assuming windows, but overall good to ask).
     
  10. Offline

    sgavster

    I know spigot has a command for restarting, not sure if is uses spigot code or bukkit, though.
     
  11. Offline

    The_Doctor_123

  12. Offline

    BungeeTheCookie

    sgavster
    Spigot does use Bukkit code, thats why it waits to update until Bukkit updates. Thats also why Bukkit plugins are compatible in Spigot.
    1Rogue
    I am using a windows, but I want it to be accessible across all platforms, CentOS, Linux, Ubuntu, Mac, Multicraft Server Host etc.
    The_Doctor_123
    How can you use a process to restart a server? Can you provide like a code example?
     
  13. Offline

    sgavster

    BungeeTheCookie I mean if the restart command uses 100% bukkit, so that you could use it w/o spigot..
     
  14. Offline

    1Rogue


    I meant more scheduling with the batch file, and then executing it with code. e.g.
    Code:
    at 2:05 run "C:\Server\run.bat"
    Or something along those lines. To contain this within java would depend on the system in use, but you could directly use:

    Code:java
    1. Runtime rt = Runtime.getRuntime();
    2. Process pr = rt.exec("at 2:05 run \"C:" + File.separatorChar + "Server " + File.separatorChar + "run.bat\"");


    Of course, it would be best to dynamically get the file name + the time, and instead of scheduling just run a task that would schedule it for you.
     
  15. Offline

    BungeeTheCookie

    sgavster
    I'm looking at the spigot code right now
     
  16. Offline

    The_Doctor_123

    BungeeTheCookie 1Rogue
    Skimming through Runtime, I found something quite useful.
    Code:java
    1. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable()
    2. {
    3. @Override
    4. public void run()
    5. {
    6. // Create new process and junk.
    7. }
    8. }));

    So apparently all shutdown hooks get called when the process is about to die, which is exactly what you want for when you start up the server.
     
  17. Offline

    BungeeTheCookie

    The_Doctor_123
    Thanks, I also found that in the Spigot code, which I will post in a second how the Restart process works.

    sgavster The_Doctor_123 1Rogue
    This is the process that Spigot uses for it's /restart command. Anyway where you can transform it into Bukkit and make it more "simpler?"

    Restart Class:
    Code:java
    1. import java.io.File;
    2. import java.util.List;
    3. import net.minecraft.server.EntityPlayer;
    4. import net.minecraft.server.MinecraftServer;
    5. import net.minecraft.server.Packet255KickDisconnect;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8.  
    9. public class RestartCommand extends Command
    10. {
    11.  
    12. public RestartCommand(String name)
    13. {
    14. super( name );
    15. this.description = "Restarts the server";
    16. this.usageMessage = "/restart";
    17. this.setPermission( "bukkit.command.restart" );
    18. }
    19.  
    20. @Override
    21. public boolean execute(CommandSender sender, String currentAlias, String[] args)
    22. {
    23. if ( testPermission( sender ) )
    24. {
    25. restart();
    26. }
    27. return true;
    28. }
    29.  
    30. public static void restart()
    31. {
    32. try
    33. {
    34. final File file = new File( SpigotConfig.restartScript );
    35. if ( file.isFile() )
    36. {
    37. System.out.println( "Attempting to restart with " + SpigotConfig.restartScript );
    38.  
    39. // Kick all players
    40. for ( EntityPlayer p : (List< EntityPlayer>) MinecraftServer.getServer().getPlayerList().players )
    41. {
    42. p.playerConnection.networkManager.queue( new Packet255KickDisconnect( SpigotConfig.restartMessage ) );
    43. p.playerConnection.networkManager.d();
    44. }
    45. // Give the socket a chance to send the packets
    46. try
    47. {
    48. Thread.sleep( 100 );
    49. } catch ( InterruptedException ex )
    50. {
    51. }
    52. // Close the socket so we can rebind with the new process
    53. MinecraftServer.getServer().ag().a();
    54.  
    55. // Give time for it to kick in
    56. try
    57. {
    58. Thread.sleep( 100 );
    59. } catch ( InterruptedException ex )
    60. {
    61. }
    62.  
    63. // Actually shutdown
    64. try
    65. {
    66. MinecraftServer.getServer().stop();
    67. } catch ( Throwable t )
    68. {
    69. }
    70.  
    71. // This will be done AFTER the server has completely halted
    72. Thread shutdownHook = new Thread()
    73. {
    74. @Override
    75. public void run()
    76. {
    77. try
    78. {
    79. String os = System.getProperty( "os.name" ).toLowerCase();
    80. if ( os.contains( "win" ) )
    81. {
    82. Runtime.getRuntime().exec( "cmd /c start " + file.getPath() );
    83. } else
    84. {
    85. Runtime.getRuntime().exec( new String[]
    86. {
    87. "sh", file.getPath()
    88. } );
    89. }
    90. } catch ( Exception e )
    91. {
    92. e.printStackTrace();
    93. }
    94. }
    95. };
    96.  
    97. shutdownHook.setDaemon( true );
    98. Runtime.getRuntime().addShutdownHook( shutdownHook );
    99. } else
    100. {
    101. System.out.println( "Startup script '" + SpigotConfig.restartScript + "' does not exist! Stopping server." );
    102. }
    103. System.exit( 0 );
    104. } catch ( Exception ex )
    105. {
    106. ex.printStackTrace();
    107. }
    108. }
    109. }


    SpigotConfig Class:
    Code:java
    1. import com.google.common.base.Throwables;
    2. import java.io.File;
    3. import java.io.IOException;
    4. import java.lang.reflect.InvocationTargetException;
    5. import java.lang.reflect.Method;
    6. import java.lang.reflect.Modifier;
    7. import java.util.ArrayList;
    8. import java.util.Arrays;
    9. import java.util.Collections;
    10. import java.util.HashMap;
    11. import java.util.List;
    12. import java.util.Map;
    13. import java.util.logging.Level;
    14. import java.util.regex.Pattern;
    15. import java.util.regex.PatternSyntaxException;
    16. import net.minecraft.server.MinecraftServer;
    17. import org.bukkit.Bukkit;
    18. import org.bukkit.ChatColor;
    19. import org.bukkit.command.Command;
    20. import org.bukkit.configuration.file.YamlConfiguration;
    21. import org.bukkit.craftbukkit.command.TicksPerSecondCommand;
    22.  
    23. public class SpigotConfig
    24. {
    25.  
    26. private static final File CONFIG_FILE = new File( "spigot.yml" );
    27. private static final String HEADER = "This is the main configuration file for Spigot.\n"
    28. + "As you can see, there's tons to configure. Some options may impact gameplay, so use\n"
    29. + "with caution, and make sure you know what each option does before configuring.\n"
    30. + "For a reference for any variable inside this file, check out the Spigot wiki at\n"
    31. + "[URL]http://www.spigotmc.org/wiki/spigot-configuration/\n[/URL]"
    32. + "\n"
    33. + "If you need help with the configuration or have any questions related to Spigot,\n"
    34. + "join us at the IRC or drop by our forums and leave a post.\n"
    35. + "\n"
    36. + "IRC: #spigot @ irc.esper.net ( [URL]http://webchat.esper.net/?channel=spigot[/URL] )\n"
    37. + "Forums: [URL]http://www.spigotmc.org/forum/\n[/URL]";
    38. /*========================================================================*/
    39. static YamlConfiguration config;
    40. static int version;
    41. static Map<String, Command> commands;
    42. /*========================================================================*/
    43. private static Metrics metrics;
    44.  
    45. public static void init()
    46. {
    47. config = YamlConfiguration.loadConfiguration( CONFIG_FILE );
    48. config.options().header( HEADER );
    49. config.options().copyDefaults( true );
    50.  
    51. commands = new HashMap<String, Command>();
    52.  
    53. version = getInt( "config-version", 3 );
    54. set( "config-version", 3 );
    55. readConfig( SpigotConfig.class, null );
    56. }
    57.  
    58. public static void registerCommands()
    59. {
    60. for ( Map.Entry<String, Command> entry : commands.entrySet() )
    61. {
    62. MinecraftServer.getServer().server.getCommandMap().register( entry.getKey(), "Spigot", entry.getValue() );
    63. }
    64.  
    65. if ( metrics == null )
    66. {
    67. try
    68. {
    69. metrics = new Metrics();
    70. metrics.start();
    71. } catch ( IOException ex )
    72. {
    73. Bukkit.getServer().getLogger().log( Level.SEVERE, "Could not start metrics service", ex );
    74. }
    75. }
    76. }
    77.  
    78. static void readConfig(Class<?> clazz, Object instance)
    79. {
    80. for ( Method method : clazz.getDeclaredMethods() )
    81. {
    82. if ( Modifier.isPrivate( method.getModifiers() ) )
    83. {
    84. if ( method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE )
    85. {
    86. try
    87. {
    88. method.setAccessible( true );
    89. method.invoke( instance );
    90. {
    91. Throwables.propagate( ex.getCause() );
    92. } catch ( Exception ex )
    93. {
    94. Bukkit.getLogger().log( Level.SEVERE, "Error invoking " + method, ex );
    95. }
    96. }
    97. }
    98. }
    99.  
    100. try
    101. {
    102. config.save( CONFIG_FILE );
    103. } catch ( IOException ex )
    104. {
    105. Bukkit.getLogger().log( Level.SEVERE, "Could not save " + CONFIG_FILE, ex );
    106. }
    107. }
    108.  
    109. private static void set(String path, Object val)
    110. {
    111. config.set( path, val );
    112. }
    113.  
    114. private static boolean getBoolean(String path, boolean def)
    115. {
    116. config.addDefault( path, def );
    117. return config.getBoolean( path, config.getBoolean( path ) );
    118. }
    119.  
    120. private static int getInt(String path, int def)
    121. {
    122. config.addDefault( path, def );
    123. return config.getInt( path, config.getInt( path ) );
    124. }
    125.  
    126. private static <T> List getList(String path, T def)
    127. {
    128. config.addDefault( path, def );
    129. return (List<T>) config.getList( path, config.getList( path ) );
    130. }
    131.  
    132. private static String getString(String path, String def)
    133. {
    134. config.addDefault( path, def );
    135. return config.getString( path, config.getString( path ) );
    136. }
    137.  
    138. public static boolean preventProxies;
    139. private static void preventProxies()
    140. {
    141. preventProxies = getBoolean( "settings.prevent-proxies", false );
    142. }
    143.  
    144. private static void tpsCommand()
    145. {
    146. commands.put( "tps", new TicksPerSecondCommand( "tps" ) );
    147. }
    148.  
    149. public static class Listener
    150. {
    151.  
    152. public String host;
    153. public int port;
    154. public boolean netty;
    155. public long connectionThrottle;
    156.  
    157. public Listener(String host, int port, boolean netty, long connectionThrottle)
    158. {
    159. this.host = host;
    160. this.port = port;
    161. this.netty = netty;
    162. this.connectionThrottle = connectionThrottle;
    163. }
    164. }
    165. public static List<Listener> listeners = new ArrayList<Listener>();
    166. public static int nettyThreads;
    167. private static void listeners()
    168. {
    169. listeners.clear(); // We don't rebuild listeners on reload but we should clear them out!
    170.  
    171. Map<String, Object> def = new HashMap<String, Object>();
    172. def.put( "host", "default" );
    173. def.put( "port", "default" );
    174. def.put( "netty", true );
    175. // def.put( "throttle", "default" );
    176.  
    177. config.addDefault( "listeners", Collections.singletonList( def ) );
    178. for ( Map<String, Object> info : (List<Map<String, Object>>) config.getList( "listeners" ) )
    179. {
    180. String host = (String) info.get( "host" );
    181. if ( "default".equals( host ) )
    182. {
    183. host = Bukkit.getIp();
    184. } else
    185. {
    186. throw new IllegalArgumentException( "Can only bind listener to default! Configure it in server.properties" );
    187. }
    188. int port ;
    189.  
    190. if (info.get( "port" ) instanceof Integer){
    191. throw new IllegalArgumentException( "Can only bind port to default! Configure it in server.properties");
    192. } else{
    193. port = Bukkit.getPort();
    194. }
    195. boolean netty = (Boolean) info.get( "netty" );
    196. // long connectionThrottle = ( info.get( "throttle" ) instanceof Number ) ? ( (Number) info.get( "throttle" ) ).longValue() : Bukkit.getConnectionThrottle();
    197. listeners.add( new Listener( host, port, true, Bukkit.getConnectionThrottle() ) );
    198. }
    199. if ( listeners.size() != 1 )
    200. {
    201. throw new IllegalArgumentException( "May only have one listener!" );
    202. }
    203.  
    204. nettyThreads = getInt( "settings.netty-threads", 3 );
    205. }
    206. public static List<String> bungeeAddresses = Arrays.asList( new String[]
    207. {
    208. "127.0.0.1"
    209. } );
    210. public static boolean bungee = true;
    211. private static void bungee()
    212. {
    213. bungeeAddresses = getList( "settings.bungeecord-addresses", bungeeAddresses );
    214. bungee = getBoolean( "settings.bungeecord", true );
    215. }
    216.  
    217. public static List<String> spamExclusions;
    218. private static void spamExclusions()
    219. {
    220. spamExclusions = getList( "commands.spam-exclusions", Arrays.asList( new String[]
    221. {
    222. "/skill"
    223. } ) );
    224. }
    225.  
    226. public static boolean logCommands;
    227. private static void logCommands()
    228. {
    229. logCommands = getBoolean( "commands.log", true );
    230. }
    231.  
    232. public static boolean tabComplete;
    233. private static void tabComplete()
    234. {
    235. tabComplete = getBoolean( "commands.tab-complete", true );
    236. }
    237.  
    238. public static String whitelistMessage;
    239. public static String unknownCommandMessage;
    240. public static String serverFullMessage;
    241. public static String outdatedClientMessage;
    242. public static String outdatedServerMessage;
    243. private static String transform(String s)
    244. {
    245. return ChatColor.translateAlternateColorCodes( '&', s ).replaceAll( "\\n", "\n" );
    246. }
    247. private static void messages()
    248. {
    249. whitelistMessage = transform( getString( "messages.whitelist", "You are not whitelisted on this server!" ) );
    250. unknownCommandMessage = transform( getString( "messages.unknown-command", "Unknown command. Type \"/help\" for help." ) );
    251. serverFullMessage = transform( getString( "messages.server-full", "The server is full!" ) );
    252. outdatedClientMessage = transform( getString( "messages.outdated-client", "Outdated client!" ) );
    253. outdatedServerMessage = transform( getString( "messages.outdated-server", "Outdated server!" ) );
    254. }
    255.  
    256. public static List<Pattern> logFilters;
    257. private static void filters()
    258. {
    259. List<String> def = Arrays.asList( new String[]
    260. {
    261. "^(.*)(/login)(.*)$"
    262. } );
    263. logFilters = new ArrayList<Pattern>();
    264.  
    265. for ( String regex : (List<String>) getList( "settings.log-filters", def ) )
    266. {
    267. try
    268. {
    269. logFilters.add( Pattern.compile( regex ) );
    270. } catch ( PatternSyntaxException ex )
    271. {
    272. Bukkit.getLogger().log( Level.WARNING, "Supplied filter " + regex + " is invalid, ignoring!", ex );
    273. }
    274. }
    275.  
    276. Bukkit.getLogger().setFilter( new LogFilter() );
    277. }
    278.  
    279. public static int timeoutTime = 60;
    280. public static boolean restartOnCrash = true;
    281. public static String restartScript = "./start.sh";
    282. public static String restartMessage;
    283. private static void watchdog()
    284. {
    285. timeoutTime = getInt( "settings.timeout-time", timeoutTime );
    286. restartOnCrash = getBoolean( "settings.restart-on-crash", restartOnCrash );
    287. restartScript = getString( "settings.restart-script", restartScript );
    288. restartMessage = transform( getString( "messages.restart", "Server is restarting" ) );
    289. commands.put( "restart", new RestartCommand( "restart" ) );
    290. WatchdogThread.doStart( timeoutTime, restartOnCrash );
    291. }
    292. }


    WatchDogThread Class:

    Code:java
    1. import java.lang.management.ManagementFactory;
    2. import java.lang.management.MonitorInfo;
    3. import java.lang.management.ThreadInfo;
    4. import java.util.logging.Level;
    5. import java.util.logging.Logger;
    6. import net.minecraft.server.MinecraftServer;
    7. import org.bukkit.Bukkit;
    8.  
    9. public class WatchdogThread extends Thread
    10. {
    11.  
    12. private static WatchdogThread instance;
    13. private final long timeoutTime;
    14. private final boolean restart;
    15. private volatile long lastTick;
    16. private volatile boolean stopping;
    17.  
    18. private WatchdogThread(long timeoutTime, boolean restart)
    19. {
    20. super( "Spigot Watchdog Thread" );
    21. this.timeoutTime = timeoutTime;
    22. this.restart = restart;
    23. }
    24.  
    25. public static void doStart(int timeoutTime, boolean restart)
    26. {
    27. if ( instance == null )
    28. {
    29. instance = new WatchdogThread( timeoutTime * 1000L, restart );
    30. instance.start();
    31. }
    32. }
    33.  
    34. public static void tick()
    35. {
    36. instance.lastTick = System.currentTimeMillis();
    37. }
    38.  
    39. public static void doStop()
    40. {
    41. if ( instance != null )
    42. {
    43. instance.stopping = true;
    44. }
    45. }
    46.  
    47. @Override
    48. public void run()
    49. {
    50. while ( !stopping )
    51. {
    52. //
    53. if ( lastTick != 0 && System.currentTimeMillis() > lastTick + timeoutTime )
    54. {
    55. Logger log = Bukkit.getServer().getLogger();
    56. log.log( Level.SEVERE, "The server has stopped responding!" );
    57. log.log( Level.SEVERE, "Please report this to [URL]http://www.spigotmc.org/[/URL]" );
    58. log.log( Level.SEVERE, "Be sure to include ALL relevant console errors and Minecraft crash reports" );
    59. log.log( Level.SEVERE, "Spigot version: " + Bukkit.getServer().getVersion() );
    60. //
    61. log.log( Level.SEVERE, "------------------------------" );
    62. log.log( Level.SEVERE, "Server thread dump (Look for plugins here before reporting to Spigot!):" );
    63. dumpThread( ManagementFactory.getThreadMXBean().getThreadInfo( MinecraftServer.getServer().primaryThread.getId(), Integer.MAX_VALUE ), log );
    64. log.log( Level.SEVERE, "------------------------------" );
    65. //
    66. log.log( Level.SEVERE, "Entire Thread Dump:" );
    67. ThreadInfo[] threads = ManagementFactory.getThreadMXBean().dumpAllThreads( true, true );
    68. for ( ThreadInfo thread : threads )
    69. {
    70. dumpThread( thread, log );
    71. }
    72. log.log( Level.SEVERE, "------------------------------" );
    73.  
    74. if ( restart )
    75. {
    76. RestartCommand.restart();
    77. }
    78. break;
    79. }
    80.  
    81. try
    82. {
    83. sleep( 10000 );
    84. } catch ( InterruptedException ex )
    85. {
    86. interrupt();
    87. }
    88. }
    89. }
    90.  
    91. private static void dumpThread(ThreadInfo thread, Logger log)
    92. {
    93. if ( thread.getThreadState() != State.WAITING )
    94. {
    95. log.log( Level.SEVERE, "------------------------------" );
    96. //
    97. log.log( Level.SEVERE, "Current Thread: " + thread.getThreadName() );
    98. log.log( Level.SEVERE, "\tPID: " + thread.getThreadId()
    99. + " | Suspended: " + thread.isSuspended()
    100. + " | Native: " + thread.isInNative()
    101. + " | State: " + thread.getThreadState() );
    102. if ( thread.getLockedMonitors().length != 0 )
    103. {
    104. log.log( Level.SEVERE, "\tThread is waiting on monitor(s):" );
    105. for ( MonitorInfo monitor : thread.getLockedMonitors() )
    106. {
    107. log.log( Level.SEVERE, "\t\tLocked on:" + monitor.getLockedStackFrame() );
    108. }
    109. }
    110. log.log( Level.SEVERE, "\tStack:" );
    111. //
    112. StackTraceElement[] stack = thread.getStackTrace();
    113. for ( int line = 0; line < stack.length; line++ )
    114. {
    115. log.log( Level.SEVERE, "\t\t" + stack[line].toString() );
    116. }
    117. }
    118. }
    119. }


    Thanks :D

    Also
    The_Doctor_123
    How can you make the server turn back on by using the shutdown hook?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  18. Offline

    Shevchik

    Learn ProcessBuilder
     
  19. Offline

    BungeeTheCookie

    Shevchik
    Example please? I usually learn more by examples.
     
  20. Offline

    Shevchik

    Code:java
    1.  
    2. ProcessBuilder pb = new ProcessBuilder();
    3. String jarfilename = Bukkit.class.getResource("").getFile();
    4. jarfilename = jarfilename.substring(0, jarfilename.indexOf(".jar"));
    5. jarfilename = new File(jarfilename).getName()+".jar";
    6. List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
    7. List<String> execsequence = new ArrayList<String>();
    8. execsequence.add("java");
    9. execsequence.addAll(arguments);
    10. execsequence.add("-jar");
    11. execsequence.add(jarfilename);
    12. pb.command(execsequence);
    13. pb.start();
    14.  
     
  21. Offline

    BungeeTheCookie

    Shevchik
    Thanks! Let's hope this works!
     
  22. Offline

    WolfLeader116

    Hello I have a question for you. Would it be possible to take this code and create a plugin out of it? If so, please make a plugin as many people would like it! If there already is a plugin for this, please give me a link to it. If you can't make a plugin, please tell me how to use the code to make the server restart without using the /stop command and instead using a command like /restart. Thanks! (If I have double-posted, I am sorry because my other post disappeared today [at least it was gone for me I don't know about everybody else] and I would like to get a response about this.)
     
  23. Offline

    BungeeTheCookie

    Well, there is a /restart command built into the Spigot (which isn't supported here), but there is a way to make the server shutdown. However, it will need access to the startup script in order to run. I do not know what it is on Multicraft, but in most cases for Windows, the startup script is "run.bat" or "start.bat" or "launch.bat". On Mac it is "launch.sh" or "start.sh" or "launch.sh" usually, so you would just have to make a shutdown hook and do all this other stuff. I will actually make a resource for this.
     
  24. Offline

    Garris0n

    I use .command on OSX, just saying.
     
  25. Offline

    BungeeTheCookie

  26. Offline

    WolfLeader116

  27. Offline

    WolfLeader116

    I found a plugin that will reload the server for you. You type /reload and it will generate a file and then will put in chat how long it will take to reload. Then, it stops the server. You edit your BAT file (sadly) to make it automatically restart the server. BUT, it will check if the file that the plugin generated was there and if it is not, the server doesn't restart. If it is there, the server starts again and the file is deleted. The plugin is Legendary Restart by IcyRelic. This allows for /stop to work properly at least...
    (Not as good as this method here could be though.)
     
Thread Status:
Not open for further replies.

Share This Page