Skip to content

Instantly share code, notes, and snippets.

@charliebritton
Last active April 6, 2021 02:34
Show Gist options
  • Save charliebritton/4f6d8b22267f7653ff5d6a2a2e1f10f5 to your computer and use it in GitHub Desktop.
Save charliebritton/4f6d8b22267f7653ff5d6a2a2e1f10f5 to your computer and use it in GitHub Desktop.
SHOUTcast auto-restart script to restart SHOUTcast DNAS server automatically on failure. Can be tested with --test flag
#!/bin/bash
# SHOUTcast auto-restart script.
# Copyright 2020 Charlie Britton
#
# Run this script as a cronjob, setting BASEDIR to the SHOUTcast directory and
# the sc_serv binary and config files below.
# Directory SHOUTcast is in
BASEDIR=$(dirname "$0")
# Binary path
BINARY=$BASEDIR/sc_serv
# Config path
CONFIG=$BASEDIR/sc_serv.conf
# Example cron config logging just errors:
# * * * * * /path/to/shoutcast-autorestart.sh 2>>/var/log/shoutcast-autorestart.log
#
# Example cron config logging info to .log and errors to .err
# * * * * * /path/to/shoutcast-autorestart.sh 1>>/var/log/shoutcast-autorestart.log 2>>/var/log/shoutcast-autorestart.err
# You can test this script works with the --test flag.
# --- END OF CONFIG ---
echo -e "$(date --iso=s -u) [INFO] Running SHOUTcast auto-restart script now. Copyright 2020 Charlie Britton"
# Test Logic
if [[ $* == *--test* ]]
then
echo "$(date --iso=s -u) [TEST] Attempting to kill SHOUTcast."
if [[ `pgrep sc_serv` != "" ]]
then
echo "$(date --iso=s -u) [TEST] sc_serv is running. Killing now."
kill -9 $(pidof sc_serv)
sleep 1
if [[ `pgrep sc_serv` != "" ]]
then
echo "$(date --iso=s -u) [TEST] sc_serv shouldn't be running, but it is! Exiting with status 1"
exit 1
fi
else
echo "$(date --iso=s -u) [TEST] sc_serv already stopped. Continuing execution."
fi
echo -e "$(date --iso=s -u) [TEST] Beginning normal script now.\n"
fi
# Actual Logic
RESULT=`pgrep sc_serv`
if [[ $RESULT == "" ]];
then
echo -e "$(date --iso=s -u) [WARN] sc_serv not running!" 1>&2;
echo -e "$(date --iso=s -u) [INFO] Running $BINARY daemon $CONFIG now." 1>&2;
printf "$(date --iso=s -u) [INFO] "
$BINARY daemon $CONFIG
else
echo "$(date --iso=s -u) [INFO] sc_serv is running (PID: $RESULT)."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment