Pages:<< prev 1 next >>
Fledgling

GroupMembers
Posts47
JoinedFeb 16, 2006
I dont like tch, so here is a bash script, it also does some log management, keeping the last 3 logs only.
#!/bin/bash
# Set the port number (default 4000 or from first argument)
port=4000
if [ -n "$1" ]; then
port="$1"
fi
# Change to area directory
cd ../area || { echo "Failed to change directory to ../area"; exit 1; }
# Set limits, needed in ubuntu to get core files, also need to give users permissions to /var/lib/systemd/coredump/ to write dumps to
ulimit -c unlimited
# Remove shutdown.txt if it exists
[ -e shutdown.txt ] && rm -f shutdown.txt
while true; do
# Find a free log file
index=1000
while true; do
logfile="../log/${index}.log"
if [ ! -e "$logfile" ]; then
break
fi
((index++))
done
# Record starting time
date > "$logfile"
date > ../area/boot.txt
# Check if SMAUG is already running on the port
matches=$(netstat -an | grep ":$port " | grep -c LISTEN)
if [ "$matches" -ge 1 ]; then
echo "Port $port is already in use."
exit 0
fi
# Run SMAUG
../src/smaug "$port" &> "$logfile"
# Keep only the last 3 log files, delete older ones
logdir="../log"
if [ -d "$logdir" ]; then
# Find all log files sorted by modification time, oldest first
files=($(ls -1tr "$logdir"/*.log 2>/dev/null))
count=${#files[@]}
if [ "$count" -gt 3 ]; then
# Delete all but the last 3
for ((i=0; i<count-3; i++)); do
rm -f "${files[i]}"
done
fi
fi
# Restart logic: exit if shutdown.txt exists
if [ -e shutdown.txt ]; then
rm -f shutdown.txt
exit 0
fi
# Give old connections a chance to die
sleep 15
done
Pages:<< prev 1 next >>