RunBootup.sh

From The World according to Vissie
Jump to navigation Jump to search

I use this script to 1. Run when my server reboot, so run when the NetworkManager starts. But only at startup once. Then 2. make sure that the internet is working, maybe my router is not up yet after a power failure. I 3. log all the events so I can interrogate that log files later and lastly 4. when the internet is on, I send myself a message that the server is online again. Lets see and hope that this works.

vim: /etc/NetworkManager/dispatcher.d/pre-up.d/bootup.sh
#! /bin/bash
LOG_FILE=/var/log/bootup.log
echo "$(date "+%d-%m-%Y %T") : Start..." >> $LOG_FILE 2>&1
exec > >(tee -a ${LOG_FILE} )
exec 2> >(tee -a ${LOG_FILE} >&2)

App_Key='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
User_Key='uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu'
Web_Adress='https://api.pushover.net/1/messages'

FLAGFILE=/var/run/bootup_firstrun.done
IF=$1
STATUS=$2
 
case "$IF" in
    lo)
        # The loopback interface does not count.
        # only run when some other interface comes up
        exit 0
        ;;
    *)
        ;;
esac
if [ "$STATUS" = "up" ]; then 
    if [ -e $FLAGFILE ]; then
        exit 0
    else
        touch $FLAGFILE
    fi 

	# So, to tripple check, we will do a ping till we get an answer.
	UNREACHEABLE=1;
	pingRetries=0
	while [ $UNREACHEABLE -ne "0" ]; 
		do ping -q -c 1 vissie.com &> /dev/null; UNREACHEABLE=$?; sleep 1; pingRetries=$((pingRetries+1));
	done
	echo "ping done in $pingRetries tries"
	
	curl -s -F "token=$App_Key" -F "user=$User_Key" -F "message=Server just rebooted for some reason. I'm $(hostname) and today is $(date). It took me $pingRetries retries to get to the internet. " $Web_Adress

fi

echo "$(date "+%d-%m-%Y %T") : Ended..." >> $LOG_FILE 2>&1