Systemd entry, a startup & backup script

Xaero252

New Member
Jul 29, 2019
10
0
0
I run ArchLinux on my home server, which is quite powerful and connected to a fairly high end connection (enough for around 50 players without significant lag) I recently started hosting a Beta Pack A server (about 4 months ago, and now I need advice on what pack to upgrade to since Beta pack isn't getting updated...) Anyways, these scripts are mostly cannibalized from the AUR "craftbukkit" package, modified for my own personal usage. The tmux package is required to properly use these scripts and administrate the server. This service also requires you to run ftb as a seperate user account on your server, in the scripts I have that user set to be "beast" however you can use any user you choose, just be certain to change all of the usernames in the scripts below, or things won't work as intended.

Without further adieu:
Systemd entry (/usr/lib/systemd/system/feedthebeast.service)
Code:
[Unit]
Description=Feed The Beast server
 
[Service]
User=beast
ExecStart=/srv/feedthebeast/ftb.sh start
ExecStop=/srv/feedthebeast/ftb.sh stop
Type=forking
 
[Install]
WantedBy=multi-user.target

ftb.sh (server start/stop/backup script located at /srv/feedthebeast/ftb.sh in my case)
Code:
#!/bin/bash
 
BACKUPPATH="/srv/feedthebeast/backup/"
WORLDPATH="/srv/feedthebeast/A001/"
 
case "$1" in
  start)
    if [ -z "`pgrep -f -n 'FTB-Beta-A.jar'`" ]; then
 
      /usr/bin/tmux new-session -d -s ftb-console -d 'cd /srv/feedthebeast; java -Xmx6144M -Xms1024M -jar /srv/feedthebeast/FTB-Beta-A.jar nogui'
      sh /srv/feedthebeast/backup.sh & echo $! > /srv/feedthebeast/backup.pid
      if [ $? -gt 0 ]; then
        exit 1
      fi
    else
      echo "FTBServer already started"
      exit 1
    fi
    ;;
 
  stop)
    tmux send-keys -t ftb-console 'me NOTICE: Server shutting down in 5 seconds!' C-m
    sleep 5
    tmux send-keys -t ftb-console 'stop' C-m
    BACKUPID=`cat /srv/feedthebeast/backup.pid`
    pkill $BACKUPID && rm /srv/feedthebeast/backup.pid
    sleep 10
    ;;
 
  backup)
    FILE="`date +%Y%m%d%H%M`.tar.gz"
    path="$BACKUPPATH/$FILE"
    mkdir -p $BACKUPPATH
    tmux send-keys -t ftb-console 'save-off' C-m
    tmux send-keys -t ftb-console 'save-all' C-m
    tmux send-keys -t ftb-console 'me Performing 15m backup - potential for lag.' C-m
    tar -czf $path $WORLDPATH & sleep 30
    tmux send-keys -t ftb-console 'save-on' C-m
    ;;
 
  console)
    sudo -u beast tmux attach -t ftb-console
    ;;
 
  *)
    echo "usage: $0 {start|stop|backup|console} (need to run as 'beast')"
esac
 
exit 0
And finally "backup.sh" which has a 15m sleep within a loop to perform forced 15m backups of the world. In my case, I have all backups older than 2 hours moved to a storage array (from a solid state disk) and any backups older than 1 day are purged entirely. You can edit this script as you see fit, it is located in my case at /srv/feedthebeast/backup.sh, and is called by "ftb.sh" when starting the server, and is cleanly shutdown when stopping the server.
(/srv/feedthebeast/backup.sh)
Code:
#!/bin/sh
cd /srv/feedthebeast
while true; do
        sleep 15m
        find ./backup/ -type f -mmin +120 | xargs -I % mv % /media/array/ftbbackups/
        find /media/array/ftbbackups/ -type f -mtime +1 -delete
        sh ./ftb.sh backup
done
If you are familiar with ArchLinux, and know how systemd works to some extent, you realize that after adding the systemd entry, you can call "# systemctl enable feedthebeast.service" and that will make the server automatically start on boot. Similarly "#systemctl disable feedthebeast.service" will disable the server from being started on bot, and "# systemctl start (or stop) feedthebeast.service" will start, or stop the server, and finally "#systemctl restart feedthebeast.service" will restart the server daemon.
You may notice that this will cause no terminal output from the server jar to be displayed - this is intended. To attach the server's console you can run "/srv/feedthebeast/ftb.sh console" and to leave the console you simply press "Ctrl+B" and then type ":detach" and press enter. You can also run "sudo -u beast tmux attach" to attach the console.
 
  • Like
Reactions: MirrorV