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.
#
# Example cron config logging everything:
# * * * * * /path/to/this/file >> /var/log/shoutcast-autorestart.log >> /var/log/shoutcast-autorestart.log
#
# Example cron config logging just errors:
# * * * * * /root/sc-server/shoutcast-autorestart.sh 2>>/var/log/shoutcast-autorestart.log
# Directory SHOUTcast is in
BASEDIR=$(dirname "$0")
# Binary path
BINARY=$BASEDIR/sc_serv
# Config path
CONFIG=$BASEDIR/sc_serv.conf
echo -e "$(date --iso=s -u) [INFO] Running SHOUTcast auto-restart script now. Copyright 2020 Charlie Britton"
# Kills SHOUTcast then runs the rest of the script.
if [[ $* == *--test* ]]
then
echo "$(date --iso=s -u) [TEST] Attempting to kill SHOUTcast."
# Logic if SHOUTcast is running
if [[ `pgrep sc_serv` != "" ]]
then
echo "$(date --iso=s -u) [TEST] sc_serv is running. Killing now."
kill -9 $(pidof sc_serv)
sleep 1
# Check it has exited
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
# Lgoic if SHOUTcast already stopped
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
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