Init.d Scripts

I decided to make my own init.d script for my lilDevil app (instead of using a startup cronjob). The “init” in init.d stands for initialization and the “.d” indicates the script is a daemon process. A daemon process is commonly defined as simply something that runs in the background. So an init.d script, is one that is called on startup and runs in the background without any user controls.

And by startup I mean apps that run on Startup 😉

Setup

Making an init.d script is simple. First create the script in the init.d directory.

vim /etc/init.d/<NAME OF YOUR STARTUP SERVICE>

Next, add executive privileges to the script.

chmod +x /etc/init.d/<NAME OF YOUR STARTUP SERVICE>

Now it’s time for the inner-workings.

Script Parts

This is ultimately a bash script so it requires this little sh-bang at the top.

#!/bin/bash

Next, a service requires a LSB header. This header is used to configure startup settings and describe the service. This tutorial goes into detail on the different available settings that can be included in the header. Below is a sample of a more basic header that will add a service to the end of the startup boot order sequence.

### BEGIN INIT INFO
# Provides:          <SERVICE NAME>
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     S
# Default-Stop:      0 1 6
# Short-Description: <LONG DESCRIPTION OF SERVICE>
# Description:
<SHORT DESCRIPTION OF SERVICE>
### END INIT INFO

The following code describes the different service actions. For instance, common actions are “start”, “stop”, “restart”, etc.

case "$1" in
<ACTION1>)

<COMMANDS/SCRIPTS TO RUN RELATING TO ACTION1>
;;
<ACTION2>)
<COMMANDS/SCRIPTS TO RUN RELATING TO ACTION2>
 ;;

<REPEAT AS NECESSARY>

Concluding, we exit and clean everything up.

exit 1
esac
exit 0

Putting it All Together

To help you see the big picture, here is a copy of my init.d script.

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          lilDevil
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     S
# Default-Stop:      0 1 6
# Short-Description: Unleash the lilDevil
# Description:       lilDevil is an awesome network enumeration tool
### END INIT INFO 
#
case "$1" in
start)
echo "Starting lilDevil "
python /root/Projects/dirtBag.py&
python /root/Projects/demon/manage.py flush --noinput
python /root/Projects/demon/manage.py runserver 192.168.1.6:3707&
python /root/Projects/sensor.py&
;;
stop)
echo "Stopping lilDevil "
killall -9 python
;;
restart)
echo "Restarting lilDevil "
killall -9 python
python /root/Projects/dirtBag.py&
python /root/Projects/demon/manage.py flush --noinput
python /root/Projects/demon/manage.py runserver 192.168.1.6:3707&
python /root/Projects/sensor.py&
;;
*)
echo "Service"
echo $"Usage: $0 {start|stop|status}"
exit 1
esac
exit 0

Configuring it to Run

With the script completed, we pass it to insserv to be added to the Linux service boot order.

insserv -d /etc/init.d/lilDevil

Tah Dah! The service is added!

What about apps with GUIs?

Now, if your app has a GUI involved, it needs to be added to the desktop environment initialization. This is different from an init.d script. Init scripts run for the most part before the desktop has been initialized.

In Kali, I simply used their tool to add the service to startup. The tool can be found under Application Menu->Settings->Session and Startup. Under the Application Autostart tab, you can add a new command to run when the desktop loads.

Screen Shot 2014-01-26 at 12.21.58 PM

That’s it!

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.