Upgpkg: 1.8.9-2

Check whether sudo is required for command execution
Replace [ with [[ (bash)
This commit is contained in:
Gordian Edenhofer 2016-01-26 20:30:17 +01:00
parent 130c71b396
commit 562fc844e8
3 changed files with 54 additions and 44 deletions

View File

@ -1,5 +1,5 @@
# Generated by mksrcinfo v8 # Generated by mksrcinfo v8
# Mon Jan 25 23:18:20 UTC 2016 # Tue Jan 26 19:30:17 UTC 2016
pkgbase = minecraft-server pkgbase = minecraft-server
pkgdesc = Minecraft server unit files, script, and jar pkgdesc = Minecraft server unit files, script, and jar
pkgver = 1.8.9 pkgver = 1.8.9
@ -29,7 +29,7 @@ pkgbase = minecraft-server
md5sums = c644abdf293c2f98033a64d732a4945c md5sums = c644abdf293c2f98033a64d732a4945c
md5sums = 5ed78e366146e47f8498347e93ad5423 md5sums = 5ed78e366146e47f8498347e93ad5423
md5sums = 8088e2b05ef4d85049dea41585a9070d md5sums = 8088e2b05ef4d85049dea41585a9070d
md5sums = 4b9ee8074249619c01ca6e3672fc329e md5sums = b10ce9918eefba8b5966e971d5ea4775
pkgname = minecraft-server pkgname = minecraft-server

View File

@ -26,7 +26,7 @@ md5sums=('3acbaef956308c805e8e2d0a03a737e9'
'c644abdf293c2f98033a64d732a4945c' 'c644abdf293c2f98033a64d732a4945c'
'5ed78e366146e47f8498347e93ad5423' '5ed78e366146e47f8498347e93ad5423'
'8088e2b05ef4d85049dea41585a9070d' '8088e2b05ef4d85049dea41585a9070d'
'4b9ee8074249619c01ca6e3672fc329e') 'b10ce9918eefba8b5966e971d5ea4775')
package() { package() {
install -Dm644 minecraftd.conf "${pkgdir}/etc/conf.d/minecraft" install -Dm644 minecraftd.conf "${pkgdir}/etc/conf.d/minecraft"

View File

@ -2,6 +2,9 @@
source /etc/conf.d/minecraft || echo "Could not source /etc/conf.d/minecraft" source /etc/conf.d/minecraft || echo "Could not source /etc/conf.d/minecraft"
# General rule for the variable-naming-schema:
# Variables in capital letters may be passed through the command line others not.
# You may use this script for any minecraft server of your choice, just alter the config file # You may use this script for any minecraft server of your choice, just alter the config file
SERVER_ROOT="${SERVER_ROOT:-/srv/minecraft}" SERVER_ROOT="${SERVER_ROOT:-/srv/minecraft}"
BACKUPPATH="${BACKUPPATH:-/srv/minecraft/backup}" BACKUPPATH="${BACKUPPATH:-/srv/minecraft/backup}"
@ -21,33 +24,40 @@ JAVA_PARMS="${JAVA_PARMS:-"-Xmx${MAXHEAP} -Xms${MINHEAP} -XX:ParallelGCThreads=$
# The actual program name # The actual program name
declare -r myname="minecraftd" declare -r myname="minecraftd"
# Check whether sudo is needed at all
if [[ $(whoami) == ${MC_USER} ]]; then
SUDO_CMD=""
else
SUDO_CMD="sudo -u ${MC_USER}"
fi
# Check for sudo rigths # Check for sudo rigths
if [ $(sudo whoami) != "root" ]; then if [[ $(${SUDO_CMD} whoami) != ${MC_USER} ]]; then
echo "You must have sudo access in order to use this script." echo -e "You have \e[39;1mno permission\e[0m to run commands as $MC_USER user."
exit 1 exit 1
fi fi
# Pipe any given argument to the minecraft server console # Pipe any given argument to the minecraft server console
mc_command() { mc_command() {
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -X stuff "`printf \"$*\r\"`" ${SUDO_CMD} screen -S "${SESSION_NAME}" -X stuff "`printf \"$*\r\"`"
} }
# Start the server if it is not already running # Start the server if it is not already running
server_start() { server_start() {
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
echo "A screen ${SESSION_NAME} session is already running. Please close it first." echo "A screen ${SESSION_NAME} session is already running. Please close it first."
else else
echo -en "Starting server... " echo -en "Starting server... "
sudo -u "${MC_USER}" screen -dmS "${SESSION_NAME}" /bin/bash -c "cd '${SERVER_ROOT}'; java ${JAVA_PARMS} -jar '${SERVER_ROOT}/${MAIN_JAR}' nogui" ${SUDO_CMD} screen -dmS "${SESSION_NAME}" /bin/bash -c "cd '${SERVER_ROOT}'; java ${JAVA_PARMS} -jar '${SERVER_ROOT}/${MAIN_JAR}' nogui"
echo -e "\e[39;1m done\e[0m" echo -e "\e[39;1m done\e[0m"
fi fi
} }
# Stop the server gracefully by saving everything prior and warning the users # Stop the server gracefully by saving everything prior and warning the users
server_stop() { server_stop() {
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
mc_command save-all mc_command save-all
mc_command say "Server is going down in 10 seconds! HURRY UP WITH WATHEVER YOU ARE DOING!" # Warning the users mc_command say "Server is going down in 10 seconds! HURRY UP WITH WATHEVER YOU ARE DOING!" # Warning the users
echo -en "Server is going down in... " echo -en "Server is going down in... "
@ -65,13 +75,13 @@ server_stop() {
# Print whether the server is running and if so give some information about memory usage and threads # Print whether the server is running and if so give some information about memory usage and threads
server_status() { server_status() {
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
echo -e "Status:\e[39;1m running\e[0m" echo -e "Status:\e[39;1m running\e[0m"
# Calculating memory usage # Calculating memory usage
for p in $(sudo -u "${MC_USER}" pgrep -f "${MAIN_JAR}"); do for p in $(${SUDO_CMD} pgrep -f "${MAIN_JAR}"); do
ps -p$p -O rss | tail -n1; ps -p${p} -O rss | tail -n1;
done | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =", count, "(screen, bash,", count-2, "x java)"; print "Total memory usage =", sum/1024, "MB" ;};' done | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =", count, "(screen, bash,", count-2, "x java)"; print "Total memory usage =", sum/1024, "MB" ;};'
else else
echo -e "Status:\e[39;1m stopped\e[0m" echo -e "Status:\e[39;1m stopped\e[0m"
@ -80,8 +90,8 @@ server_status() {
# Restart the complete server by shutting it down and starting it again # Restart the complete server by shutting it down and starting it again
server_restart() { server_restart() {
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
server_stop server_stop
sleep 0.1 sleep 0.1
server_start server_start
@ -94,30 +104,30 @@ server_restart() {
backup_files() { backup_files() {
# Check for the availability of the tar binaries # Check for the availability of the tar binaries
which tar &> /dev/null which tar &> /dev/null
if [ $? -ne 0 ]; then if [[ $? -ne 0 ]]; then
echo "The tar binaries are needed for a backup." echo "The tar binaries are needed for a backup."
exit 2 exit 2
fi fi
echo "Starting backup..." echo "Starting backup..."
FILE="$(date +%Y_%m_%d_%H.%M.%S).tar.gz" FILE="$(date +%Y_%m_%d_%H.%M.%S).tar.gz"
sudo -u "${MC_USER}" mkdir -p "${BACKUPPATH}" ${SUDO_CMD} mkdir -p "${BACKUPPATH}"
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
mc_command save-off mc_command save-off
mc_command save-all mc_command save-all
sync && wait sync && wait
sudo -u "${MC_USER}" tar -C "${SERVER_ROOT}" -czf "${BACKUPPATH}/${FILE}" --totals "${WORLDPATHS}" 2>&1 | grep -v "tar: Removing leading " ${SUDO_CMD} tar -C "${SERVER_ROOT}" -czf "${BACKUPPATH}/${FILE}" --totals "${WORLDPATHS}" 2>&1 | grep -v "tar: Removing leading "
mc_command save-on mc_command save-on
else else
sudo -u "${MC_USER}" tar -C "${SERVER_ROOT}" -czf "${BACKUPPATH}/${FILE}" --totals "${WORLDPATHS}" 2>&1 | grep -v "tar: Removing leading " ${SUDO_CMD} tar -C "${SERVER_ROOT}" -czf "${BACKUPPATH}/${FILE}" --totals "${WORLDPATHS}" 2>&1 | grep -v "tar: Removing leading "
fi fi
echo -e "\e[39;1mbackup completed\e[0m\n" echo -e "\e[39;1mbackup completed\e[0m\n"
echo -n "Only keeping the last ${KEEP_BACKUPS} backups and removing the other ones..." echo -n "Only keeping the last ${KEEP_BACKUPS} backups and removing the other ones..."
BACKUP_COUNT=$(for f in "${BACKUPPATH}"/[0-9_.]*; do echo $f; done | wc -l) BACKUP_COUNT=$(for f in "${BACKUPPATH}"/[0-9_.]*; do echo ${f}; done | wc -l)
if [ $(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) -gt 0 ]; then if [[ $(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) -gt 0 ]]; then
sudo -u "${MC_USER}" rm $(for f in "${BACKUPPATH}"/[0-9_.]*; do echo $f; done | head -n$(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS})) ${SUDO_CMD} rm $(for f in "${BACKUPPATH}"/[0-9_.]*; do echo ${f}; done | head -n$(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}))
echo -e "\e[39;1m done\e[0m ($(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) backup(s) pruned)" echo -e "\e[39;1m done\e[0m ($(expr ${BACKUP_COUNT} - ${KEEP_BACKUPS}) backup(s) pruned)"
else else
echo -e "\e[39;1m done\e[0m (no backups pruned)" echo -e "\e[39;1m done\e[0m (no backups pruned)"
@ -128,20 +138,20 @@ backup_files() {
backup_restore() { backup_restore() {
# Check for the availability of the tar binaries # Check for the availability of the tar binaries
which tar &> /dev/null which tar &> /dev/null
if [ $? -ne 0 ]; then if [[ $? -ne 0 ]]; then
echo "The tar binaries are needed for a backup." echo "The tar binaries are needed for a backup."
exit 2 exit 2
fi fi
# Only allow the user to restore a backup if the server is down # Only allow the user to restore a backup if the server is down
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
echo -e "The \e[39;1mserver should be down\e[0m in order to restore the world data." echo -e "The \e[39;1mserver should be down\e[0m in order to restore the world data."
exit 3 exit 3
fi fi
# Either let the user choose a backup or expect one as an argument # Either let the user choose a backup or expect one as an argument
if [ $# -lt 1 ]; then if [[ $# -lt 1 ]]; then
echo "Please enter the corresponding number for the backup to be restored: " echo "Please enter the corresponding number for the backup to be restored: "
i=1 i=1
for f in "${BACKUPPATH}"/[0-9_.]*; do for f in "${BACKUPPATH}"/[0-9_.]*; do
@ -151,13 +161,13 @@ backup_restore() {
echo -en "Restore backup number: " echo -en "Restore backup number: "
# Read in user input # Read in user input
read USER_CHOICE read user_choice
# Interpeting the input # Interpeting the input
if [[ $USER_CHOICE =~ ^-?[0-9]+$ ]]; then if [[ $user_choice =~ ^-?[0-9]+$ ]]; then
n=1 n=1
for f in "${BACKUPPATH}"/[0-9_.]*; do for f in "${BACKUPPATH}"/[0-9_.]*; do
[[ $n -eq $USER_CHOICE ]] && FILE="$f" [[ ${n} -eq $user_choice ]] && FILE="$f"
n=$((n+1)) n=$((n+1))
done done
if [[ -z $FILE ]]; then if [[ -z $FILE ]]; then
@ -170,10 +180,10 @@ backup_restore() {
fi fi
elif [[ $# -eq 1 ]]; then elif [[ $# -eq 1 ]]; then
# Check for the existance of the specified file # Check for the existance of the specified file
if [ -f "$1" ]; then if [[ -f "$1" ]]; then
FILE="$1" FILE="$1"
else else
if [ -f "${BACKUPPATH}"/"$1" ]; then if [[ -f "${BACKUPPATH}"/"$1" ]]; then
FILE="${BACKUPPATH}"/"$1" FILE="${BACKUPPATH}"/"$1"
else else
echo -e "Sorry, but '$1', is \e[39;1mnot a valid file\e[0m, neither in your current directory nor in the backup folder." echo -e "Sorry, but '$1', is \e[39;1mnot a valid file\e[0m, neither in your current directory nor in the backup folder."
@ -187,7 +197,7 @@ backup_restore() {
fi fi
echo "Restoring backup..." echo "Restoring backup..."
sudo -u "${MC_USER}" tar -xf "${FILE}" -C "${SERVER_ROOT}" 2>&1 ${SUDO_CMD} tar -xf "${FILE}" -C "${SERVER_ROOT}" 2>&1
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
echo -e "\e[39;1mRestoration completed\e[0m" echo -e "\e[39;1mRestoration completed\e[0m"
else else
@ -197,17 +207,17 @@ backup_restore() {
# Run the given comman at the minecraft server console # Run the given comman at the minecraft server console
server_command() { server_command() {
if [ $# -lt 1 ]; then if [[ $# -lt 1 ]]; then
echo "No server command specified. Try 'help' for a list of commands." echo "No server command specified. Try 'help' for a list of commands."
exit 1 exit 1
fi fi
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
sleep 0.1s & sleep 0.1s &
SLEEP_PID=$! sleep_pid=$!
mc_command "$@" & mc_command "$@" &
tail -f --pid=${SLEEP_PID} -n 0 "${LOGPATH}/latest.log" tail -f --pid=${sleep_pid} -n 0 "${LOGPATH}/latest.log"
else else
echo "There is no ${SESSION_NAME} session to connect to." echo "There is no ${SESSION_NAME} session to connect to."
fi fi
@ -215,9 +225,9 @@ server_command() {
# Enter the screen minecraft session # Enter the screen minecraft session
server_console() { server_console() {
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -Q select . > /dev/null ${SUDO_CMD} screen -S "${SESSION_NAME}" -Q select . > /dev/null
if [ $? -eq 0 ]; then if [[ $? -eq 0 ]]; then
sudo -u "${MC_USER}" screen -S "${SESSION_NAME}" -rx ${SUDO_CMD} screen -S "${SESSION_NAME}" -rx
else else
echo "There is no ${SESSION_NAME} session to connect to." echo "There is no ${SESSION_NAME} session to connect to."
fi fi