Network Safety Restart Bash Script

Date: Wed, Jun 27, 2012

Tool when working on the network settings of a remote linux server

If you ever work on a remote servers network settings then this script may safe you from having to call support, and waiting on them.

When started/executed it

  • Sleeps first for 1 hour
  • Then it renames the ”/etc/network/interfaces” file by adding the current time stamp to the end of the file
  • It renames a file called ”/etc/network/” to ”/etc/network/interfaces”
  • And finally it restarts the server.

It also warns you a couple minutes before it does all that so you can terminate the program. Of course if you have been locked out you can’t terminate it, and it will execute. Giving you a fresh start!

To start start &

To Stop stop

Here is the linkto the Gist or you can just copy and paste from below.

#! /bin/sh


. /lib/lsb/init-functions

export PIDFILESTART=/tmp/
export PIDFILESTOP=/tmp/
export FILE=/etc/network/interfaces

case "$1" in
    if [ -f ${PIDFILESTART} ]; then
            rm ${PIDFILESTART}
    if [ -f ${PIDFILESTOP} ]; then
            rm ${PIDFILESTOP}
    ps -fe | grep ${1} | head -n1 | cut -d" " -f 6 > ${PIDFILESTART}

        sleep 3600 

        log_action_msg "WARNING: Will in 120 sec rename ${FILE} and then restart"
        sleep 60
        log_action_msg "WARNING: Will in 60 sec rename ${FILE} and then restart"
        sleep 60

    if ! [ -f ${PIDFILESTOP} ]; then
        log_action_msg "Restarting NOW"
            SUFFIX=$(date +%s)
            cp ${FILE} ${FILE}.${SUFFIX}
            sleep 1
            cp ${FILE}.org ${FILE}
            sleep 1
            reboot -d -f -i
        rm ${PIDFILESTOP}
        log_action_msg "NOT Restaring as you wish"
    if [ -f ${PIDFILESTART} ]; then
            rm ${PIDFILESTART}
        touch ${PIDFILESTOP}
        log_action_msg "Terminating restart script"

    log_action_msg "Terminated restart script"

    exit 0
        echo "Usage: $0 start|stop" >&2
        exit 3