Link to home
Start Free TrialLog in
Avatar of lunanat
lunanatFlag for Canada

asked on

Custom script in startup causing boot hang - Ubuntu

So, as the subject says, I've written a script, and I've added it to my startup.

I have done so, using update-rc.d mainscript.sh defaults 99

When I run the script manually, it works every time.  When I have it in any of the RC files (RC.2 for example) when the server boots, it starts all of the processes that it should (apache, openBSD sshd, etc) however then it hangs.  Usually I then see OpenBSD sshd restart, and there it sits.

I realize that most scripts have start and stop commands.. could this be all it is?  Script is attached as a code snippet, in case that is what is causing it.

#!/bin/bash

function Sync {
	rsync -AErqz $othernode:/var/www /var
	rsync -AErqz $othernode:/etc/apache2/sites-available /etc/apache2/
	rsync -AEqz $othernode:/etc/apache2/*.conf /etc/apache2
}

ifup eth0 &> /dev/null
ifup eth0:1 &> /dev/null
ifdown eth0:1 &> /dev/null

URL="10.98.0.22"
Node1="10.98.0.20"
Node2="10.98.0.21"
thisnode=$(ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | grep -v '10.98.0.22' | cut -d: -f2 | awk '{ print $1}')
echo This node: $thisnode - $HOSTNAME
if [ $Node1 != $thisnode ]; then
	othernode=$Node1
else
	othernode=$Node2
fi
echo othernode: $othernode

while [ 1 ];
do
	####If the node is primary, execute the primary functionality
	if [ -f /usr/ha/primary.var ]; then
		ifdown eth0:1
		/etc/init.d/apache2 start $> /dev/null
		echo Info - Assuming I am the primary node
		pingtest=$(ping -c 1 -I eth0 -W 5 $URL &> /dev/null)
		while [ $? = 0 ]; do
			echo - Warn - Shared IP Reachable
			if ! wget -T 5 -q -O /dev/null $URL:81 ; then
				echo - - CRIT - Web Server is not responsive, and I am suppposed to be primary
			else
				echo Info - Webserver IS responsive\;  I thought I was supposed to be primary.
				echo - Warn - Stepping down to Secondary!
				touch /usr/ha/secondary.var
				rm /usr/ha/primary.var
				break
			fi
			echo - Warn - Waiting...
			sleep 15
			pingtest=$(ping -c 1 -I eth0 -W 5 $URL &> /dev/null)
		done
		if [ -f /usr/ha/primary.var ]; then
			echo Info - Shared IP is not reachable.  This is good.
			echo Info - Bringing up interface eth0:1
			ifup eth0:1
			arping -UA -c 3 -I eth0:1 10.98.0.22
		fi
		while [ -f /usr/ha/primary.var ]; do
			sleep 3
			pingtest=$(ping -c 1 -I eth0:1 -W 5 10.98.0.254)
			if [ $? != 0 ]; then
				echo - - CRIT - My eth0:1 interface is down.  Stepping down to secondary.
				ifdown eth0:1
				touch /usr/ha/secondary.var
				rm /usr/ha/primary.var
			fi
		done
	####If the node is secondary, execute the secondary functionality
	elif [ -f /usr/ha/secondary.var ]; then
		echo Info - Assuming I am the secondary node
		sleep 10
		ifdown eth0:1 &> /dev/null
		while [ 1 ]; do
			getvar=$(wget -T 5 -q -O /dev/null $URL:81)
			if [ $? != 0 ]; then
				echo Get FAILED
				break
			fi
			#### WGET succeeded, rsync data as normal
			Sync
			sleep 10
		done
		#### WGET must have failed, if we reach this
		echo - - CRIT - I am secondary, but I cannot wget the website.  Taking charge!
		ssh $othernode touch /usr/ha/secondary.var
		ssh $othernode rm /usr/ha/primary.var
		ssh $othernode ifdown eth0:1
		touch primary.var
		rm secondary.var
	fi
	sleep 2
	echo Infinite Loop!  WOOOOO!!!
done

Open in new window


At the start I ifup and ifdown a bunch, because I want to guarentee that the virtual interface is stopped when the script is starting.
 
Avatar of TobiasHolm
TobiasHolm
Flag of Sweden image

You could try to start Bash with the -l switch (to simulate a logged in user)
#!/bin/bash -l

Open in new window

Avatar of lunanat

ASKER

Thanks for the reply Tobias,

The change you suggested made no difference to the reboot behaviour, unfortunately.
Avatar of lunanat

ASKER

Aha... now I know what is causing the problem, I just don't know how to fix it.

The script runs as an infinite loop.  When I log into TTY2 and kill the script that has started, TTY1 then shows the rest of the boot process and sits ready for me to log in.

How can I tell the script to run off on its own and not hold up the boot process?
ASKER CERTIFIED SOLUTION
Avatar of TobiasHolm
TobiasHolm
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lunanat

ASKER

Thank you.