Start Application Automatically on Restart in Debootstrap Environment

By | March 26, 2013

If you install an application on a linux debian server, debian takes care of restarting the application when you restart the server. But when you start working on creating a new application on Debootstrap environment ( otherwise called as linux sandbox environment ) you can’t expect the same. On server restart, only applications installed outside the debootstrap environment gets started automatically not the applications installed inside the debootstrap ( chroot environment ). So how do we fix this? Well here is a work around.

So let’s me imagine that your debootstrap environment is located at /home/deboot/ and you want to start apache2 inside /home/deboot/. Now here are couple of script that can help you do that.

Create a script inside /etc/init.d/ and call it startapache

touch /etc/init.d/startapache

Give executable permissions to this file.

chmod +x /etc/init.d/startapache

Now, use the following script in /etc/init.d/startapache

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
AP_CHROOT=/home/deboot/
APACHE_DUP=/etc/init.d/apache_start

count=`mount | grep /home/apmanager/apmanage/proc|wc -l`
if [ $count -eq 0 ]
then
if [ -d /home/deboot/proc ]
then
mount -t proc /proc /home/deboot/proc
else
echo ” ERROR: No proc directory.”
fi
fi

chroot ${SBOX_RT} ${DAEMON} > /dev/null

chroot ${AP_CHROOT} ${APACHE_DUP} > /dev/null

Now, inside the chroot environment,

chroot /home/deboot/

Create a file “/etc/init.d/start_all.sh”

Give executable permissions to this file.

chmod +x /etc/init.d/start_all.sh

Now use the following script inside “/etc/init.d/start_all.sh” to start apache.

#!/bin/bash

/etc/init.d/apache2 start

In case you have multiple programs to be started inside chroot environment you can start them in the script /etc/init.d/start_all.sh

Now finally, to setup auto startup, go to /etc/init.d/ directory outside the sandbox and use the following command.

update-rc.d startapache defaults

The above command will add your script startapache to run when the system starts everytime.

If you like this article, then please consider sharing it. Sharing really inspires us to do things much better!!

Leave a Reply

Your email address will not be published. Required fields are marked *