Hackosis is an Open Blog. You Can Participate.

  • 02
  • Oct

init.d

Have a Linux executable that you would like to run as a daemon? Making your own init.d scripts can be a bit tricky, but I can help you out.

The directory /etc/init.d/ is a location on a Linux file system that contains scripts for changing init states.

For details on Linux run levels look here.

The run levels that are most important to us are 2, 3 and 5.

VERY basic sample init.d script (Replace italics respectively):

#!/bin/sh
### BEGIN INIT INFO
# Provides: My Daemon
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Description: My Daemon Desc
### END INIT INFO

case “$1″ in
’start’)
/path/to/my/executable -startcommand
;;
’stop’)
/path/to/my/executable -stopcommand
;;
*)
echo “Usage: $0 { start | stop }”
;;
esac
exit 0

Even though most of the top part is commented out, Linux still reads it to determine what daemons need to be running before this can start, the name and desc, and also what runlevels this is suppose to start in:

### BEGIN INIT INFO
# Provides: My Daemon
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Description: My Daemon Desc
### END INIT INFO

After writing the file save it to /etc/mydaemon. Now you should be able to stop and start your daemon with the following command:

# /etc/init.d/mydaemon start

And likewise for stopping:

# /etc/init.d/mydaemon stop

Sometimes executables in Linux are picky about what directory you call them from. I would label that as a bug, but to get around that you can enter the cd /path/to/my/executable command before your stop and start command and just write it like so:

case “$1″ in
’start’)
cd /path/to/my/executable
./executable -startcommand
;;
’stop’)
cd /path/to/my/executable
./executable -stopcommand
;;
*)
echo “Usage: $0 { start | stop }”
;;
esac
exit 0

EDIT: Don’t forget to run chkconfig –add name or update-rc.d on Debian based systems after making your init.d script.

Hopefully that will get you started so you don’t have to worry about manually starting anything when your Linux server boots. Again, this is very basic. You can also enter a restart and status function, but these are optional and would vary depending on the executable. Have fun.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related Posts


Tags: , , , , ,

Like this post? Subscibe to the RSS feed.


3 Comments

  1. HowTo: Create your own init.d scripts | Linux Love Says:

    [...] Read the rest of this entry ยป [...]

  2. DIY: Simple Chat Server with Netcat | Hackosis Says:

    [...] ( >> chatlog.txt). There are many other possibilities including setting this up as a daemon with an init.d script. possibilities. If you have any netcat tips or ways to implement this with OpenSSL, I would love to [...]

  3. Vyatta - No Way for a Null Route | Hackosis Says:

    [...] to enter null routes from within the Vyatta CLI. But this should work just fine when using an init.d script or entered directly in the bash shell: ip route add blackhole 192.168.0.0/16 ip route add blackhole [...]

Leave a Comment