Adding services to Linux startup

17 Dec

Linux operation is controlled by its runlevels. There are 6 different levels defined in /etc/inittab file:

# 0 – halt (Do NOT set initdefault to this)
# 1 – Single user mode
# 2 – Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 – Full multiuser mode
# 4 – unused
# 5 – X11
# 6 – reboot (Do NOT set initdefault to this)

The above line determines the default runlevel. Each runlevel has its own directory in /etc/rc.d/

Level 3 is /etc/rc.d/rc3.d/
To add an auto-start service, we have to create a start/stop/restart script in /etc/rc.d/init.d/ and then link to it from different runlevel directories. In my case, I linked the lighttpd script from rc3.d, rc0.d and rc6.d. I don’t care about runlevel 1, 2, 4 and 5, as they’ll never be used on a production web server.

Below is the lighttpd script which should be located in /etc/rc.d/init.d:

# Startup script for the Lighttpd Web Server
# chkconfig: – 85 15
# description: Lighttpd is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: lighttpd
# pidfile: /var/run/
# config: /usr/local/sbin/lighttpd.conf


case “$1” in
# Starts the lighttpd deamon
echo “Starting Lighttpd”
/usr/local/sbin/lighttpd -f $LIGHTTPD_CONF

# stops the daemon bt cat’ing the pidfile
echo “Stopping Lighttpd”
kill -9 `cat $PIDFILE`
# kills the dispatch.fcgis
ps axww | grep dispatch | egrep -v ‘grep’ | awk ‘{ print $1 }’ | xargs kill -9
## Stop the service regardless of whether it was
## running or not, start it again.
echo “Restarting Lighttpd”
$0 stop
$0 start
# reloads the config file by sending HUP
echo “Reloading config”
kill -HUP `cat $PIDFILE`
echo “Usage: lighttpdctl (start|stop|restart|reload)”
exit 1


Posted by on December 17, 2005 in Lighttpd, Linux


4 responses to “Adding services to Linux startup

  1. jain

    March 28, 2008 at 4:57 am

    Can you exactly tell me the name of the link file
    i.e ln -s /etc.rc.d/init.d/lighttpd S

    or exactly how you did it

    thanks in advance

  2. serbiancafe

    March 28, 2008 at 9:33 am

    You are welcome.

    For level 3 the command would be:
    cd /etc/rc.d/rc3.d/
    ln -s /etc/rc.d/init.d/lighttpd

  3. siva

    May 9, 2008 at 1:59 pm

    Thanks for the thread. It is really helpful. Appreciated it.

  4. Loon

    July 19, 2012 at 8:41 am

    Really helpful


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: