Skip to content

Instantly share code, notes, and snippets.

@deviationist
Created February 19, 2025 12:00
Show Gist options
  • Save deviationist/65c6db06e31c8bb41bc3466069f8d8e6 to your computer and use it in GitHub Desktop.
Save deviationist/65c6db06e31c8bb41bc3466069f8d8e6 to your computer and use it in GitHub Desktop.
A simple shell-script that will allow you to power down/up your NAS in tandem with Plex, to prevent Plex from wiping the library when the files are absent (NAS offline)
#!/bin/bash
# Configuration
NAS_HOSTNAME="your_nas_hostname" # Replace with your NAS hostname
NAS_MAC="XX:XX:XX:XX:XX:XX" # Replace with your NAS MAC address
NAS_USER="your_username" # Replace with your SSH username
PLEX_CONTAINER="plex-linuxserver" # Replace with your Plex container name
# Function to check if NAS is responding
check_nas_available() {
local max_attempts=30
local attempt=1
while [ $attempt -le $max_attempts ]; do
if ssh -q -o BatchMode=yes -o ConnectTimeout=2 "${NAS_USER}@${NAS_HOSTNAME}" "exit" 2>/dev/null; then
echo -e "\r\033[K" # Clear the line
return 0
fi
echo -ne "\r\033[KWaiting for NAS to respond... (attempt $attempt/$max_attempts)"
sleep 10
((attempt++))
done
echo -e "\r\033[K" # Clear the line
echo "Timeout waiting for NAS to respond"
return 1
}
# Function to check system status
system_status() {
echo "=== System Status ==="
# Check NAS status
echo -n "NAS Status: "
if ssh -q -o BatchMode=yes -o ConnectTimeout=2 "${NAS_USER}@${NAS_HOSTNAME}" "exit" 2>/dev/null; then
echo "ONLINE"
# Get NAS uptime
echo -n "NAS Uptime: "
ssh "${NAS_USER}@${NAS_HOSTNAME}" "uptime"
else
echo "OFFLINE"
fi
# Check Plex container status
echo -n "Plex Status: "
if docker ps | grep -q "${PLEX_CONTAINER}"; then
echo "RUNNING"
# Get container uptime and version
echo "Plex Container Info:"
docker ps --format "Started: {{.RunningFor}}\nImage: {{.Image}}" -f name="${PLEX_CONTAINER}"
else
echo "STOPPED"
fi
}
# Function to bring system down
system_down() {
# Check if Plex container is running
if docker ps | grep -q "${PLEX_CONTAINER}"; then
echo "Stopping Plex container..."
docker stop "${PLEX_CONTAINER}"
else
echo "Plex container is already stopped"
fi
# Check if NAS is responding before trying to shut it down
if ssh -q -o BatchMode=yes -o ConnectTimeout=2 "${NAS_USER}@${NAS_HOSTNAME}" "exit" 2>/dev/null; then
echo "Shutting down NAS..."
ssh -t "${NAS_USER}@${NAS_HOSTNAME}" "sudo poweroff"
else
echo "NAS appears to be already offline"
fi
}
# Function to bring system up
system_up() {
# Check if NAS is already responding
if ssh -q -o BatchMode=yes -o ConnectTimeout=2 "${NAS_USER}@${NAS_HOSTNAME}" "exit" 2>/dev/null; then
echo "NAS is already online"
else
echo "Sending WOL packet to NAS..."
wakeonlan "$NAS_MAC"
echo "Waiting for NAS to boot..."
if ! check_nas_available; then
echo "Failed to connect to NAS"
exit 1
fi
echo "NAS is up, remounting filesystems..."
sudo mount -a
fi
# Check if Plex container is already running
if docker ps | grep -q "${PLEX_CONTAINER}"; then
echo "Plex container is already running"
else
echo "Starting Plex container..."
docker start "${PLEX_CONTAINER}"
fi
}
# Main script
case "$1" in
"up")
# Run in background with nohup if not already running in nohup
if [ -z "$NOHUP_RUNNING" ]; then
export NOHUP_RUNNING=1
nohup "$0" up &
exit 0
fi
system_up
;;
"down")
system_down
;;
"status")
system_status
;;
*)
echo "Usage: $0 {up|down|status}"
echo " up - Start NAS and services (runs in background)"
echo " down - Stop services and shutdown NAS"
echo " status - Show system status"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment