Linux Bash Script That Continues After Server Reboot

Today I would like to show you something that others have asked me alot about recently and that is the ability to run either or series of bash scripts or one bash script with multiple functions after a system has been rebooted.

You might be wondering “Why would I want to do this?” A great example that comes to mind is patching. For automated kernel upgrades and security patching.

The key is to using logic to place a line of code into /etc/rc.local

IE

if [[ $(echo $POST) != "0" ]]
    then
        echo -e "Please review the attached yumUpdateStatus.log file which shows what packages were installed\n"
	echo -e "sh /root/oel_qtr_update.sh post-update > /dev/null 2>&1" >> /etc/rc.local
	 bootServer
fi

The above code will make the next portion of upgrade happen after the reboot. Again there is more logic required to make everything work. As part of the process I’ve written, after the reboot while it performs post-update steps there are status checks. If certain criteria are met and the system is healthy it will remove the line from /etc/rc.local

IE

if [[ $(echo $yumExitCode) != "0" ]]
    then
        if [[ $(echo $yumExitFail) != "0" ]]
            then
                echo -e "UPDATES FAILED - `date |awk '{print $2,$3,$4}'`"
            else
                echo -e "NO UPDATES AVAILABLE - `date | awk '{print $2,$3,$4}'`"
        fi
    else
        if [[ $(echo $POST) = "0" ]]
            then
                echo -e "NO UPDATES AVAILABLE - `date | awk '{print $2,$3,$4}'`"
                sed -i '/oel_qtr_update/d' /etc/rc.local
                cleanUp
                bootServer
        fi
fi

Its a quick way to do script continuation. You can certainly apply this to other activities, not just for OS updates.

Hope this provides an explanation.

Leave a Reply

Your email address will not be published.