Skip to content

Instantly share code, notes, and snippets.

@Lu5ck
Last active January 24, 2024 20:07
Show Gist options
  • Save Lu5ck/1312711d725538a32a4be8de8722d476 to your computer and use it in GitHub Desktop.
Save Lu5ck/1312711d725538a32a4be8de8722d476 to your computer and use it in GitHub Desktop.
Palworld - Dedicated Linux Server Scripts
#Script to check for available memory
#If available memory is below certain amount, it will initiate shutdown
#The shutdown is done via rcon https://github.com/gorcon/rcon-cli
#You can get this script to be execute in crontab
#!/bin/bash
lock_file="/home/Palworld/Palworld/check_mem_leak.lock"
rcon_yaml="/home/Palworld/Palworld/rcon.yaml"
rcon="/home/Palworld/Palworld/rcon"
memAvailable=$(grep MemAvailable /proc/meminfo | awk '{print $2}')
COUNTDOWNTIME=600 # Seconds to shutdown
# If no running process
if pgrep PalServer-Linux -u $(id -un) > /dev/null 2>&1; then
exit 0
fi
if [ -e "$lock_file" ]; then
exit 0
fi
if [ $memAvailable -lt 1000000 ]; then
touch "$lock_file"
$rcon -c $rcon_yaml "Broadcast Memory_leak_threshold_has_reached"
$rcon -c $rcon_yaml "Broadcast Restarting_server_in_$(($COUNTDOWNTIME / 60))min"
# How many 5 minutes?
a=$(($COUNTDOWNTIME / 300))
while [ $a -gt 1 ]; do # We announce every 5 minutes
# Get to the next 5 mod minutes
if [ $(($COUNTDOWNTIME % 300)) -ne 0 ]; then
sleep $(($COUNTDOWNTIME - ( 300 * $a )))
COUNTDOWNTIME=$((300 * $a))
$rcon -c $rcon_yaml "Broadcast Restarting_server_in_$(($a * 5))min"
else
sleep 300
COUNTDOWNTIME=$(($COUNTDOWNTIME - 300))
a=$(($COUNTDOWNTIME / 300))
$rcon -c $rcon_yaml "Broadcast Restarting_server_in_$(($a * 5))min"
fi
done
# How many 1 minutes?
a=$(($COUNTDOWNTIME / 60))
while [ $a -gt 1 ]; do # We announce every minute
sleep 60
COUNTDOWNTIME=$(($COUNTDOWNTIME - 60))
a=$(($COUNTDOWNTIME / 60))
$rcon -c $rcon_yaml "Broadcast Restarting_server_in_$(($a * 1))min"
done
$rcon -c $rcon_yaml "Broadcast Manual_Saving"
$rcon -c $rcon_yaml "Save"
sleep 28
$rcon -c $rcon_yaml "Broadcast Initiating_Inbuilt_Auto_Shutdown"
$rcon -c $rcon_yaml "Shutdown"
sleep 33
rm "$lock_file"
fi
#Script to start the server in detached screen
#Require screen installed
#!/bin/bash
if ! pgrep PalServer-Linux -u $(id -un) > /dev/null 2>&1; then
screen -d -m /home/Palworld/Palworld/wrapper.sh
else
echo "Server already running"
fi
#Script wrapper to wrap running Palworld
#By wrapping Palworld, this script is able to know when it shut down
#It persistently keep the server running unless variable in wrapper.conf is set to true
#It also backup the save files using tar and lz4 every time it shutdown
#!/bin/bash
sed_escape() {
sed -e 's/[]\/$*.^[]/\\&/g'
}
cfg_read() { # path, key -> value
test -f "$1" && grep "^$(echo "$2" | sed_escape)=" "$1" | sed "s/^$(echo "$2" | sed_escape)=//" | tail -1
}
cfg_write() { # path, key, value
cfg_delete "$1" "$2"
echo "$2=$3" >> "$1"
}
cfg_delete() { # path, key
test -f "$1" && sed -i "/^$(echo $2 | sed_escape).*$/d" "$1"
}
if [ ! -e "/home/Palworld/Palworld/wrapper.conf" ]; then
touch /home/Palworld/Palworld/wrapper.conf
fi
cfg_write /home/Palworld/Palworld/wrapper.conf SHUTDOWN false
while true; do
/home/Palworld/Palworld/PalServer.sh EpicApp=PalServer -useperfthreads -NoAsyncLoadingThread -UseMultithreadForDS -port=53021 -publicport=53021
sleep 2
# Make sure there's a backup directory created
mkdir -p /home/Palworld/Palworld/Pal/Backup
timestamp=$(date +%Y%m%d%H%M%S)
cd "/home/Palworld/Palworld/Pal/Saved/"
tar cf - "SaveGames" | lz4 > "/home/Palworld/Palworld/Pal/Backup/${timestamp}.tar.lz4"
sleep 1
echo $(cfg_read /home/Palworld/Palworld/wrapper.conf SHUTDOWN)
# If shutdown command is issued
if [ "$(cfg_read /home/Palworld/Palworld/wrapper.conf SHUTDOWN)" == "true" ]; then
break
fi
if pgrep PalServer-Linux -u $(id -un) > /dev/null 2>&1; then
break
fi
done
@Lu5ck
Copy link
Author

Lu5ck commented Jan 22, 2024

I use +force_install_dir to install Palworld so the directory can be different from others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment