Bukkit as a service

Discussion in 'Bukkit Discussion' started by Emirin, Jan 7, 2011.

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

    Emirin

    Any way you can set up the install to set up as a service instead of having to use a wrapper? This would be very advantageous to our development of mineadmin for windows boxes. Then again that would be windows only...
     
  2. Offline

    ChadTheDJ

    I would like that as well where I can have the server run in startup without having to add it in the start up after I log in. Would be extremely useful.
     
  3. Offline

    Emirin

  4. Offline

    Silentspy

    moved to appropriate forum.
     
  5. Offline

    Kekec852

    Ther is more g33k way using Apache Daemon (http://commons.apache.org/daemon/)
    But for this method you need to implement an interface Daemon.
    Something like this (this sample is for hMod (for Bukkit version i need to look at Bukkit/CraftBukkit code)):


    Code:
    public class MinecraftDaemon implements Daemon{
        private MinecraftServer server = new MinecraftServer();
        private Thread mainProgram = new Thread(server);
        public void init(DaemonContext dc) throws DaemonInitException, Exception {
            dc.getController();
        }
    
        public void start() throws Exception {
            mainProgram.start();
        }
    
        public void stop() throws Exception {
            server.a();
        }
    
        public void destroy() {
            mainProgram.stop();
        }
    }
    When you prep this code you need to comple/get prgram called jsvc(linux/mac)/procrun(windows)

    I never try running this on windows but it should't be that hard to make it happen.
    Here is some more info: http://commons.apache.org/daemon/procrun.html

    On linux/mac you need to put few command in to place wher it will launche it on start-up/shutdown.
    Code:
    jsvc -jvm server -outfile </full/path/to/file> -errfile </full/path/to/file> -pidfile </full/path/to/file> -cp commons-daemon.jar:</full/path/to/minecraftserver/jarfiles> -Xms1024M -Xmx1024M package.of.your.class.DaemoneClassName
    Note: you need to download commons-daemon.jar form Apache Daemon Project page.
    For stoping server you need to execute:
    Code:
    jsvc -stop -pidfile </full/path/to/file>
    
    More info about linux/mac part: http://commons.apache.org/daemon/jsvc.html

    Hope this will help someone.
     
  6. Offline

    Obsidian

    Kekec, on linux it isn't necessary in the first place. Using init scripts (and bash scripts) along with crontabs, you can easily run Minecraft/Bukkit as a background process and not need an ssh connection open.

    Requires the use of "screen" however.
     
  7. Offline

    Kekec852

    Or you could do that. But wiht my method you can easily add extenstion like running daemon as normal user just by adding additional paramtert to jsvc, adding ability to plugins to restart server from within game and so on.

    I agree using other methods can be easier.
     
  8. Offline

    Chewi

    In my Gentoo Linux package for the Minecraft server, I use "tmux" rather than "screen" because access to it is simply determined by the permissions on the socket file. I have it so that anyone in the games group can access it.

    I was concerned that shutting down the server externally would cause data to be lost. The description of Apache Daemon seems to suggest this would happen. But I did a simple experiment of mining a block immediately before shutting down the server and when I loaded it back up, that change had indeed been saved. That doesn't guarantee that data will never be lost but it would probably never be catastrophic.
     
  9. Offline

    Obsidian

    Regardless, it's best to get the minecraft server to stop itself instead. Piping through the stop command will guarantee a proper shutdown and should prevent loss of data.
     
  10. Offline

    Chewi

    There's a lot of Gentoo-specific stuff here but if you're curious, here are the scripts...
    http://overlays.gentoo.org/proj/java/browser/java-overlay/games-server/minecraft-server/files
    --- merged: Jan 8, 2011 3:19 PM ---
    Yeah I wondered whether it would be possible to do that but I didn't look too deeply into it.
     
  11. Offline

    Kekec852

    With Apache Daemon version i call stop method of server (server.a();). The same method is call wehen you type stop into console so ... if it works wiht stop command this shoud work too. It prints same output as console stop command.

    I'm using this method for about a month with no data losses(that i would notice).
     
  12. Offline

    Chewi

    I've now read your post about Apache Daemon more closely. I didn't realise you'd wrapped it around the existing server. This approach would be ideal for Gentoo so I'll definitely try it once I've got my head around Bukkit. Thanks!
     
  13. Offline

    Kekec852

    Note i forget to write how to stop server. I add it in original post.
     
  14. Offline

    4am

    I'm interested in using tmux over screen for my Ubuntu setup (tmux is, imho, much better for working interactively with terminals) but I found one large disadvantage: Is it possible to send commands into the tmux session? With screen, you can do "screen -X stuff" but I can't find an tmux equivalent.

    Edit: 5 minutes later and i stumble on send-keys in the manpages. d'oh. Any other tips you can give for working with tmux in this situation? Is there a way to configure tmux under the UID the server runs as so that users who connect to the console (at this time, only myself, but this could change in the future) can't change the layout or create new panels/windows?
     
  15. Offline

    Chewi

    I wrote about tmux way back in January.

    When using send-keys, you need to add ^M on the end of your command for a carriage return. With screen, you need to type this as an escape code. With tmux, you can literally type ^M and it will still work. Excuse what I said there, I tried it again just now and it doesn't seem to work any more. You do need to type it as an escape code.

    To share between users, set the socket path to a shared location like /tmp/tmux-minecraft using the -S option. If I remember rightly, using the -S option automatically sets the socket file to have chmod 660, at least on recent versions of tmux. If this doesn't work, try upgrading. If the socket file is owned by games:games then users in the games group should have access. This is how I do it for my Gentoo packages.

    Another useful escape code I forgot to mention is ^U. Put this at the start of the command to clear the line first. This is important because someone else might have just typed something.

    Since this thread has been dragged back up, I'll add something about Apache Daemons, which was talked about earlier. Don't use the code posted above with CraftBukkit because it already performs a safe stop using a simpler technique. If you do use this code with vanilla Minecraft then leave out the mainProgram.stop() line. It shouldn't be there and will cause an exception when the server stops. If you want to use the same technique as CraftBukkit then use this code instead. I'll admit it's not well tested yet but it's worked so far.

    Code:
    import net.minecraft.server.MinecraftServer;
    
    public class MinecraftWrapper {
        private static MinecraftServer server = new MinecraftServer();
    
        public static void main(String args[]) {
            Runtime.getRuntime().addShutdownHook(new Thread() {
                public void run() {
                    server.a();
                }
            });
    
            server.run();
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 8, 2016
Thread Status:
Not open for further replies.

Share This Page