Files
homarr/scripts/run.sh
T
Thomas Camlong 03f0d12b32 feat: report container RAM usage to PostHog every 30 minutes
Adds a background loop in run.sh that reads cgroup v2 memory stats
and sends a `ram_usage` event to PostHog EU. This lets us track
real-world memory footprint across releases.

Configurable via RAM_REPORT_INTERVAL env var (defaults to 1800s).
2026-05-07 19:02:00 +02:00

90 lines
2.9 KiB
Bash

#!/usr/bin/env bash
# Create sub directories in volume
mkdir -p /appdata/db
mkdir -p /appdata/redis
mkdir -p /appdata/trusted-certificates
# Run migrations
if [ "$DB_MIGRATIONS_DISABLED" = "true" ]; then
echo "DB migrations are disabled, skipping"
else
echo "Running DB migrations"
# We disable redis logs during migration as the redis client is not yet started
DISABLE_REDIS_LOGS=true node ./db/migrations/$DB_DIALECT/migrate.cjs ./db/migrations/$DB_DIALECT
fi
# Auth secret is generated every time the container starts as it is required, but not used because we don't need JWTs or Mail hashing
export AUTH_SECRET=$(openssl rand -base64 32)
# Support secret file for encryption key (is not empty and file exists)
if [ -n "$SECRET_ENCRYPTION_KEY_FILE" ] && [ -f "$SECRET_ENCRYPTION_KEY_FILE" ]; then
export SECRET_ENCRYPTION_KEY=$(cat $SECRET_ENCRYPTION_KEY_FILE)
fi
# Start nginx proxy
# 1. Replace the HOSTNAME in the nginx template file
# 2. Create the nginx configuration file from the template
# 3. Start the nginx server
export HOSTNAME
envsubst '${HOSTNAME}' < /etc/nginx/templates/nginx.conf > /etc/nginx/nginx.conf
# Start services in the background and store their PIDs
nginx -g 'daemon off;' &
NGINX_PID=$!
if [ "$REDIS_IS_EXTERNAL" = "true" ]; then
echo "Using external Redis server at redis://$REDIS_HOST:$REDIS_PORT"
REDIS_PID=""
else
echo "Starting internal Redis server"
redis-server /app/redis.conf &
REDIS_PID=$!
fi
node apps/nextjs/server.js &
NEXTJS_PID=$!
report_ram() {
INTERVAL=${RAM_REPORT_INTERVAL:-1800}
while true; do
sleep "$INTERVAL"
MEM_USED_BYTES=$(cat /sys/fs/cgroup/memory.current)
MEM_MAX_RAW=$(cat /sys/fs/cgroup/memory.max)
MEM_USED_MB=$((MEM_USED_BYTES / 1048576))
if [ "$MEM_MAX_RAW" = "max" ]; then
MEM_MAX_MB=0
else
MEM_MAX_MB=$((MEM_MAX_RAW / 1048576))
fi
wget -q --post-data="{\"api_key\":\"phc_pWxeD1hbl4ip02JYReX1Crjkt5DhB3dduigirHMCtFE\",\"event\":\"ram_usage\",\"distinct_id\":\"${HOSTNAME:-homarr}\",\"properties\":{\"ram_used_mb\":${MEM_USED_MB},\"ram_max_mb\":${MEM_MAX_MB},\"ram_used_bytes\":${MEM_USED_BYTES},\"\$process_person_profile\":false}}" \
--header="Content-Type: application/json" \
-O /dev/null https://hog.ajnart.dev/i/v0/e/ 2>/dev/null
echo "Reported RAM: ${MEM_USED_MB}MB / ${MEM_MAX_MB}MB"
done
}
report_ram &
REPORT_RAM_PID=$!
# Function to handle SIGTERM and shut down services
terminate() {
echo "Received SIGTERM. Shutting down..."
kill -TERM $REPORT_RAM_PID $NGINX_PID $NEXTJS_PID 2>/dev/null
wait
# kill redis-server last because of logging of other services and only if $REDIS_PID is set
if [ -n "$REDIS_PID" ]; then
kill -TERM $REDIS_PID 2>/dev/null
wait
fi
echo "Shutdown complete."
exit 0
}
# When SIGTERM (docker stop <container>) / SIGINT (ctrl+c) is received, run the terminate function
trap terminate TERM INT
# Wait for all processes
wait $NEXTJS_PID
terminate