Server Auto-Restart Script

B

BlueHead

Guest
Hi my name is BlueHead and recently a friend of mine who runs a FTB server (Infinity Evolved) for a few weeks wanted me to restart the server automatically every 6 hours, i thought it could help some people out there. So let's get started.

First Part : Getting the Server Started

Our server isn't using screen like most, but instead we're using tmux which is a terminal multiplexer that i find more reliable and user-friendly.

To start the server I'm using a little sub-script. i recommend to put both the script used here in the same directory the server is in for me it will be "/minecraft" consider replacing this with the path to your directory.

Here is the code :
Code:
#!/bin/bash

# create the new tmux session for the server

tmux new-session -d -s ftb

# tmux send is short for send-keys
# move to the directory and launch the FTB usual ServerStart inside the session

tmux send -t ftb cd Space /minecraft Enter
tmux send -t ftb sh Space ServerStart.sh Enter

#@author BlueHead

I'll explain line by line,

First : tmux new-session -d -s ftb

Basically this line create a new terminal sessions ( new-session ) without attaching to it ( -d ) named ftb ( -s ftb ).

Second : tmux send -t ftb cd Space /minecraft Enter

This line is using send which is short for send-keys, it allows you to send keys input to sessions that you created
the syntax is simple, you write down what you want to send with some special key name like Space, Enter, ...

So, this line is sending keys input ( send ) to the session ftb ( -t ftb ), -t stands for target
and the input is "cd /minecraft" so with the send syntax it is "cd Space /minecraft Enter"

Take care with the Enter key at the end, without it your inputs will be sent to the sessions but won't be executed as the Enter key isn't pressed. if you miss one it might end up screwing your whole script.

Third : tmux send -t ftb sh Space ServerStart.sh Enter

The third line isn't really different, the only thing that changes is the input sent to the session.
here we're just executing the usual script that starts the server, in my case ServerStart.sh

You would write "sh ServerStart.sh" and it end up looking like "sh Space ServerStart.sh Enter" with the send syntax

If we recap, this start script create a new session, and inside this session move to the server directory and execute the usual start script


Second Part : Rebooting the server

This is the script that is executed every 6 hours

Here is the code

Code:
#!/bin/bash

# verbose to prevent people connected to the server
tmux send -t ftb say Space Server Space is Space going Space through Space REBOOT Space phase Space Enter

# timer start for people to finish their activities
tmux send -t ftb say Space Restarting  Space in Space 30 Space Seconds Enter

sleep 10

tmux send -t ftb say Space Restarting  Space in Space 20 Space Seconds Enter

sleep 10

tmux send -t ftb say Space Restarting  Space in Space 10 Space Seconds Enter

sleep 5

tmux send -t ftb say Space Restarting  Space in Space 5 Space Seconds Enter

sleep 1

tmux send -t ftb say Space Restarting  Space in Space 4 Space Seconds Enter

sleep 1

tmux send -t ftb say Space Restarting  Space in Space 3 Space Seconds Enter

sleep 1

tmux send -t ftb say Space Restarting  Space in Space 2 Space Seconds Enter

sleep 1

tmux send -t ftb say Space Restarting  Space in Space 1 Space Seconds Enter

sleep 1

tmux send -t ftb say Space Restarting  Space in Space 0 Space Seconds Enter

tmux send -t ftb say Space Server Space REBOOTING Space now Space ! Enter

# rebooting the server using the reboot routine
tmux send -t ftb reboot Enter
sleep 20
# killing the actual tmux used by the server

tmux kill-session -t ftb

# moving to directory and executing the start ( see start.sh comments)

cd /minecraft
sh start.sh

#@author BlueHead


first : Most of the line here look the same I'll explain one and you'll get the other ones

tmux send -t ftb say Space [ Your text here]
sleep [Your sleep time here]


We're again sending keys input to the session but as you saw, we're sending "say" which will broadcast a message from the server, it is used here to warn people that the server is going down. the syntax is the same as in other send lines

The sleep command, basically stop the script for a certain time, by default it is seconds, so it stop the script for example, 5 seconds then execute the next instruction, it is here used for the timer before the shutdown.

Second : tmux send -t ftb reboot Enter

We're sending "reboot" as an input, which will reboot the server, nothing fancy here

Third : tmux kill-session -t ftb

Here we're killing the session that the server used, because the start script create a fresh new one.

Fourth :

cd /minecraft
sh start.sh

Simply, moving to directory and executing the start.sh script we've seen at the start of this post.


Third Part : Run it every 6 hour ?

Yeah, we have the scripts running but still it doesn't execute every 6 hours.

Hopefully, we have something really useful for that, Crontab, short for chrono table, which is basically a planification table, were you put your script or commands to execute and the time, using the correct syntax.

for an easy way to use the crontab i recommend using crontab generators on the net

to modify it, execute crontab -e

add your line that you generated or created by hand at the end of the file and save it. No need to restart
the syntax is explained in the file itself.

the line i use ( to execute it every 6 hours )

0 * */6 * * * /minecraft/restart.sh


Well it looks like we're done, we have our script duo that run every six hours.

If you want to go further i would recommend checking the man page and cheatsheet for tmux you can find them everywhere on Internet

I hope it can help some of you, Have a nice Minecraft Day.
 
V

VIP_CreaTive

Guest
Hello BlueHead,

I tried this Scripts today and unfortunately, I got an Error that the Sleep Command is not a real Command.
The Say Command works fine.
Do you have any suggestions for me?

Thanks
VIP_CreaTive
 

grandrolf

Global moderator
Team Member
Global Moderator
Trusted User
Aug 29, 2014
2,658
246
133
sweden
OP hasn't been online since this post was made, so you will probably not get a answer from OP.

Can you show some screen shots and perhaps share your script files as well?

Use paste.feed-the-beast.com to share the scripts (copy paste and share the link) - and I'll try and help you out.
 
  • Like
Reactions: VIP_CreaTive
V

VIP_CreaTive

Guest
Hello grandrolf,
thanks for your fast reply.
It was my fault after writing the file in the Nano-Editor and not Notepad++ it worked.
The Problem was Return-Escapes that got added with Notepad++.

Have a nice day and a great Christmas.
 
  • Like
Reactions: grandrolf