Hi All! So, I'm new to Nim and fairly new to programming in general. I'm building a script that will start up and maintain my Minecraft Server. However I'm having trouble with timing. The machine I'm hosting the server on is linux, however, I'm building the script on my Windows machine, so it should also work on there. Here is basically what I want to do: The script will be set to start when the computer starts. It will download the latest version of the server and start it. However, at a certain time, say 2:00 AM, I want the server to shut-down and preform maintenance. I've already thought about using the time module and just doing this:
while Time < TargetTime
doStuff()
But I'm afraid it would be a hog on the cpu, and it doesn't need to check every tick (is that the proper term?). How would I go about this?
Sidenote: Also, how would I go about sending commands to a already running command? For example, to stop a minecraft server properly you should use the /stop command. How would I go about starting the server using a command, and then sending a command to the same console "session" to stop the server?
Thanks for everything!
A very easy way is to "os.sleep()" for a while. See: http://nim-lang.org/docs/os.html#sleep,int
That can work good enough depending on what "doStuff()" is doing :)
Other ways are more complicated and as I need to sleep() now I leave that to others :)
I don't know anything on Minecraft servers, but I will try to give you some ideas on how to start.
I'm assuming you start the server with a console command. Programs running this way communicate via stdin and stdout: https://en.wikipedia.org/wiki/Standard_streams Everything you need for this seems to be in osproc and streams module: http://nim-lang.org/docs/osproc.html http://nim-lang.org/docs/streams.html
For graceful shutdown of the server, you can send the /stop command on the input stream of the server process. See the functions startProcess() and inputStream() and then wait with waitForExit(), all in the osproc module.
So now you still have the problem of:
The easy but "ugly" solution is to make a console program with a loop that does os.sleep(1000) and on every loop checks:
This program wakes up every 1 second for a small check, which isn't all that hoggy I think for a program that has to control 1 minecraft server. There are methods to do this without a 1second sleep. For example, you can use timers / events from your operating system. I'm not familiar with how this works in the win32 API and if this is already easily accessible in some nim standard library, but google will probably help you a lot if you want to learn about this subject.
The really nice but harder solution I think is to create a headless daemon that listens to a TCP port on your localhost. You can let it serve a nice html page that you can access from the browser, with status information of the minecraft server. You can control it with a script that sends tcp messages to the daemon, for example the command to stop the server. Or you could add buttons to the html page to control it...
Lastly you could also create a GUI program, instead of a console or daemon. Or even a GUI that issues commands to the headless daemon (this can also be done via a pipe instead of tcp).
If you are new to programming i would suggest to start with the easy solution :)
A solution in your case could be to create a cron (same as Windows scheduled tasks) on your Linux server which execute daily at 2.00am, containing the command which will execute your binary script which will do your maintenance.
But you need to find a way to communicate with your Minecraft server in order to stop it. I don't know Minecraft at all but you still need to send the /stop command, for that you can dig in Ivan's idea above. Then you can download the current version of the server, and launch it with Nim's procedures described above, then terminate your script. The next day, the cron will execute your binary script again and repeat the steps.
If you are hardcore, you could even dig in the net/asyncnet packages in order to create a socket with the server and making a sort of countdown in the chat like you can see on World of Warcraft before maintenance shutdown[1], and even why not explaining by this way why the server is restarting. To do that, you need to know the Minecraft's protocol and find the format used to send chat messages to the server. That could be tricky but if you are good in networks stuff you could sniff the packets from Minecraft when you send a chat message to see how it is done, or maybe you can find that on Internet. So you could put your cron at 1.55am and do a 5 minutes countdown. That could be fun to do. :)
[1] https://faceplantreview.files.wordpress.com/2012/03/wowscrnshot_032212_190953.jpg
This honestly might be faster to do if you simply use a bash script. Then you just have to figure out the server process id and use it's stdin/stdout to communicate.
Add that script as a cron job and you are done.
If you want more and permanent stuff as a wrapper you could have your program directly spawn the server as child process but if you really only want to restart once a day that seems a tad overkill.