[Solved] MCServer Questions: Running as Service / Running in VS
#9
For anyone who wishes to replicate the process and run MCServer as a service using the same method, this is how I accomplished it. This is a high level overview of the process with the required information. I am making the assumption that the person doing this is either comfortable with the steps, i.e. using GitHub, using Microsoft Visual Studio, modifying and compiling source code or is willing to seek out the information and do some research on any unfamiliar areas. Having knowledge about compiling source code, using Visual Studio, GitHub and working with Windows Services is very helpful. Anyone is capable of doing this if they have the tenacity to seek out the information on the Internet. To cover all of the details related to every requirement and individual step would probably be better suited for a Wiki article or other "how to" article.

Do the following:
  • Clone or download as a zip file the following winsw service wrapper from GitHub,
    https://github.com/kohsuke/winsw
  • Read the README.markdown file. This can easily be done on the GitHub site by clicking on the file name README.markdown.
  • If not installed, download and install Microsoft Visual Studio. I used Microsoft Visual Studo 2013 Express for Windows Desktop.
  • Open the solution file, winsw.sln (a Microsoft Visual Studio Solution file) in Microsoft Visual Studio and compile the solution to verify that everything is working correctly.
  • If the project compiles successfully with zero errors, then edit the main.cs file in MSVS or apply the following patch to the main.cs file.

    Code:
    --- C:\Users\Administrator\Documents\GitHub\winsw\Backup\Main.cs
    +++ C:\Users\Administrator\Documents\GitHub\winsw\Main.cs
    @@ -216,10 +216,18 @@

                 StartProcess(process, startarguments, descriptor.Executable);

    +            StreamWriter w = process.StandardInput;
    +            w.AutoFlush = true;
    +            Console.SetOut(w);
    +
    +            var handle = process.Handle;
    +            Kernel32.SetStdHandle(-10, handle); // set stdin
    +
                 // send stdout and stderr to its respective output file.
                 HandleLogfiles();

    -            process.StandardInput.Close(); // nothing for you to read!
    +        
    +            //process.StandardInput.Close(); // nothing for you to read! This line could also just be deleted.
             }

             protected override void OnShutdown()

  • If editing the main.cs file add the above lines prefixed with '+' (addition) symbol and delete or change the line above that is prefixed with a '-' (subtraction) symbol. In all honesty, this is a hack and there are better ways of accomplishing the same thing, but this works.
  • Once the changes have been made recompile the project.
  • Follow the directions for installing and using the service wrapper contained in the README.markdown.
The XML configuration file that I use for the winsw service wrapper contains the following:

Code:
<service>
  <id>MinecraftServer</id>
  <name>MinecraftServer</name>
  <description>Wrap and run MinecraftServer as a service</description>
  <env name="MCServerHome" value="%BASE%"/>
  <executable>MCServer_debug.exe</executable>
  <arguments></arguments>
  <workingdirectory>%BASE%\MCServer</workingdirectory>
  <logpath>%BASE%/logs</logpath>
  <log mode="roll-by-size">
  <sizeThreshold>20480</sizeThreshold>
  <keepFiles>10</keepFiles>
</log>
</service>

You will have to modify it based on your configuration, but it should be very similar. I have attached a .zip file that contains the service wrappper, winsw.exe and the source file, main.cs, that contains the modified C# source code. The executable is a Windows x86 33-bit requiring .Net.

Best of luck.


Attached Files
.zip   winsw.zip (Size: 28.19 KB / Downloads: 240)
Reply
Thanks given by: xoft


Messages In This Thread
RE: [Solved] MCServer Questions: Running as Service / Running in VS - by TealFrog - 09-09-2014, 01:40 PM



Users browsing this thread: 2 Guest(s)