-2

I am trying to set up a Minecraft server. However, the basic startup scripts provided do not fit my needs. I want a script that will:

  • Start a new screen running the jarfile and (pretty much) only the jarfile (so i can ^C it if needed without killing other things like screen or my gzip commands)
  • Gzip any logs that weren't gzipped automatically by the jarfile (for if/when i ^C'ed the server, or if it crashed)
  • Run a command with sudo to set the process in the first argument to a high priority (/usr/bin/oom-priority)
  • Run a http-server on the resource-pack directory in a different screen and send ^C to it when the server closes

I have these three commands. I run startserver to start the server.

startserver:

#!/bin/bash
set -m

cd /home/minecraftuser/server/

echo
screen -dm -S http-server http-server ./resource-pack
screen -dm -S my-mc-server startserver_command
(sleep 1; startserver_after) &
screen -S my-mc-server

startserver_command:

#!/bin/bash
set -m

cd /home/minecraftuser/server/

echo
java -Xmx768M -Xms768M -jar ./craftbukkit.jar $@ &
env MC_PID=$! > /dev/null
(sleep 0.5; sudo /usr/bin/oom-priority $MC_PID) &
fg 1

echo
read -n 1 -p 'Press any key to continue...'

and startserver_after:

#!/bin/bash

cd /home/minecraftuser/server/

wait $MC_PID


find /home/minecraftuser/server/logs -type f -name "*.log" -print | while read file; do gzip $file &
done

screen -S http-server -p 0 -X stuff \^c\\r

Edit: When I run startserver, I get a command prompt then a bunch of gzip errors of files already existing (I am expecting these errors, but when I run startserver I'm supposed to get the java program). Somehow I am in a screen because when I do ^A d, I am brought to a new prompt. Once I am out of the screen, screen -ls returns two instances of my-mc-server. One is a blank command prompt, the other is the server running successfully.

Edit 2: I changed startserver_command to remove the asterisk from env MC_PID=$! & (not needed there) and added it to (sleep 1; startserver_after) (makes it faster), redirected env line to /dev/null (removes entire environment listing at beginning of output). Still didn't fix the entire problem.

Coder-256
  • 5,212
  • 2
  • 23
  • 51

1 Answers1

0

Instead of starting each screen session from the scripts, you can just use a custom .screenrc to specify some startup windows (and to run commands/scripts):

#$HOME/mc-server.screenrc
screen -t http-server 0 'startserver'
screen -t my-mc-server 1 'startserver_command'
screen -t gzip-logs 2 'startserver_after'

Then simply start screen (specifying the config file to use, if it's not the default ~/.screenrc)

screen -dm -c mc-server.screenrc
Akos Bannerth
  • 1,964
  • 18
  • 14
  • That isn't exactly what I want. I want it to run the jarfile, while simultaneously running the sudo command and the http server, and once `java` exits, _then_ run gzip. – Coder-256 May 26 '14 at 20:49
  • Place the commands you want to run simultaneously in separate scripts, and specify them in the screenrc to be executed. Place the gzip command after the java command in the same script, and it will be executed after java exits (if you don't use `&` to start it in the background, which is unneeded in this case). – Akos Bannerth May 26 '14 at 21:17