Skip to content

Instantly share code, notes, and snippets.

@charliebritton
Last active April 6, 2021 02:34

Revisions

  1. charliebritton revised this gist Jul 19, 2020. 1 changed file with 14 additions and 26 deletions.
    40 changes: 14 additions & 26 deletions shoutcast-autorestart.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,10 @@
    #!/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")
    @@ -21,52 +15,46 @@ 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"

    # Kills SHOUTcast then runs the rest of the script.
    # Test Logic
    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

    # 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)."

    echo "$(date --iso=s -u) [INFO] sc_serv is running (PID: $RESULT)."
    fi
  2. charliebritton created this gist Jul 12, 2020.
    72 changes: 72 additions & 0 deletions shoutcast-autorestart.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #!/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