To add a new service on startup, you should add the necessary script to the directory /etc/init.d/. Look at the pre-exisiting scripts for an example of what to do.

Basic Example#

#! /bin/sh
# /etc/init.d/foo

# Some things that run always
touch /var/lock/foo

# Carry out specific functions when asked to by the system
case "$1" in
    echo "Starting script foo "
    echo "Could do more here"
    echo "Stopping script foo"
    echo "Could do more here"
    echo "Usage: /etc/init.d/foo {start|stop}"
    exit 1

exit 0

Once you've saved your file into the correct location make sure that it's executable by running

$ chmod 755 /etc/init.d/foo

Then you need to add the appropriate symbolic links to cause the script to be executed when the system goes down, or comes up.


The simplest way of doing this is to use the Debian-specific command update-rc.d:

root@shost:~$ update-rc.d foo defaults
 Adding system startup for /etc/init.d/foo ...
   /etc/rc0.d/K20foo -> ../init.d/foo
   /etc/rc1.d/K20foo -> ../init.d/foo
   /etc/rc6.d/K20foo -> ../init.d/foo
   /etc/rc2.d/S20foo -> ../init.d/foo
   /etc/rc3.d/S20foo -> ../init.d/foo
   /etc/rc4.d/S20foo -> ../init.d/foo
   /etc/rc5.d/S20foo -> ../init.d/foo


If you wish to remove the script from the startup sequence in the future run:

root@host:/etc/rc2.d$ update-rc.d -f foo remove
update-rc.d: /etc/init.d/foo exists during rc.d purge (continuing)
 Removing any system startup links for /etc/init.d/foo ...

This will leave the script itself in place, just remove the links which cause it to be executed.

You can find more details of this command by running man update-rc.d.

CategoryComputing.Linux - CategoryComputing.Linux.Shell