2

Me and couple of my friends are using a shared folder on Dropbox, where've uploaded our minecraft server. The point of this is that anyone can launch a server at any time, insted of one person having to run it all the time.

The only problem is, two people might launch the server at the same time, and overlapping save files might occur. To combat this, I want to write a simple batch file.

The logic is this:

  1. IF the process "minecraft_server.1.8.1" is running, rename the the server folder to "RUNNING AS _INSERT_NAME_HERE_", ELSE rename it back to "Minecraft server"

  2. The NAME would be read as a computer name (if that's at all possible), or from some txt file that a user would create (to write their own name)

I've never written a batch file and I don't know if this is possible, but it seems simple enough and any help would be appreciated.

Thank you in advance.

Ok so after a bit of tinkering, I wrote my first batch file. For the most part it seems to be working, however I can't seem to implement a proper loop.

@echo off
IF EXIST *_RUNNING.txt (
echo "ERROR, SERVER ALREADY RUNNING as %computername%"
pause
EXIT
) ELSE (
copy NUL %computername%_RUNNING.txt 
START /WAIT minecraft_server.1.8.1.exe
tasklist /FI "IMAGENAME eq javaw.exe" 2>NUL | find /I /N "javaw.exe">NUL
:loop 
IF "%ERRORLEVEL%"=="0" (
TIMEOUT /t 5
GOTO loop
) ELSE (
del %computername%_RUNNING.txt
echo "Server ended."
pause
EXIT ) )

When I start minecraft_server.1.8.1.exe it then launches javaw.exe, and that is the actual process that starts the server. After that I just check if the process is still running or not. However I can't seem to loop that particular part of the code, I keep getting synatx errors.

Saša Kalaba
  • 3,233
  • 3
  • 23
  • 43
  • possible duplicate of [How to check if a process is running via a batch script](http://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script) – stefana Feb 17 '15 at 11:54
  • 2
    By renaming the folder, you'll be triggering dropbox to reupload the contents of the folder which will cause a lot of extra network traffic and a delay while the changes are synced to all users. How about using a small temporary file (e.g. MinecraftIsRunning.txt) instead to signal that the minecraft server is running? The batch file could check for the existence of this file when you run it - if it's there just exit, otherwise create the file and run the minecraft server and have the batch file wait until the minecraft server quits – John Sibly Feb 17 '15 at 11:55

1 Answers1

3

This is not exactly what you are looking for in the topic of the question, but if I get it right you would like to avoid that a server is started twice. So I would do the following:

Create a batch-file that:

First checks if a file named *_MinecraftServer.txt exists. If yes, it assumes that the server is already running and simply exits. If no such file exists, it creates the file. Then it starts the server. Once the server exits, the file is deleted.

This would be something like this:

@echo off
REM Define a filename for the text-file informing other people that a server is running
SET FILENAME=%computername%_MinecraftServer.txt
REM Check if such a file already exists, if not refuse to start:
if exist *_MinecraftServer.txt (
  REM Such a file exists, print the content of the file:
  echo "ERROR, SERVER ALREADY RUNNING"
  type *_MinecraftServer.txt
) else (
  REM No such file is found. Create a file, then start the server process.
  echo "SERVER is running on %computername%" > %FILENAME%
  REM replace the notepad.exe with the name of the server.exe (and all    params it requires)
  REM Maybe you will also need to write the full-path to the server
  notepad.exe
  REM Once the server exits, remove the file
  del %FILENAME%
  echo "Server exited"
)
REM Require the user to hit a key before exiting the batch:
pause

This sample batch starts a notepad.exe and created a file COMPUTERNAME_MinecraftServer.txt. You would have to replace notepad.exe with the path to the minecraft server exe. Note that I'm not sure if this batch works if any of the directories involved contains spaces.

And of course this only works if from now on all people start the server only using the batch-file. Also, theoretically multiple servers can be run if multiple users start the server at more or less the same time, as there is some delay until dropbox will have uploaded and distributed the created txt-file.

erg
  • 1,530
  • 1
  • 9
  • 20
  • +1 Good job! This question is actually a great introduction to the concepts of concurrency and thread/process synchronisation – John Sibly Feb 17 '15 at 12:27
  • I tried out your code, but for whatever reason i keep getting syntax errors. – Saša Kalaba Feb 17 '15 at 16:26
  • Ahh. It appears that I accidentally typed something wrong. My mistake, it does work. However I still have a slight problem. Like stated above, it appears that the server.exe starts a javaw.exe process, and the txt file should be deleted after the javaw.exe stops running. – Saša Kalaba Feb 17 '15 at 17:50
  • If javaw.exe stops running, doesn't then server.exe also exits? Or does server.exe exits immediately? Like: server.exe starts javaw.exe and then server.exe already exits? – erg Feb 18 '15 at 10:16