Search:  
Gentoo Wiki

Initscripts


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

Introduction

rc scripts are used to launch programs which are called daemons or services. These programs are started during the boot process and do not exit until deliberately stopped. An example of a daemon is the Apache Web service or an email service, these services typically run whenever your server is running.

Gentoo includes a very sophisticated and powerful facility for managing the daemon services. This system is similar to but not compatible with the rc system that is used by other versions of *nix. The Gentoo rc scripts are quite easy to make and a simple script can be written in under 5 min.

This wiki page gives an overview of a typical rc script, for a more in-depth technical discussion be sure to also read the manual

Required Functions

Optional Functions

Example rc script

For this example we will make a script to run the "example" daemon. This example should be called /etc/init.d/example

File: /etc/init.d/example
#!/sbin/runscript

depend() {
        # the daemon needs the internet to function
        need net  
        # the daemon should run before mta
        before mta
        # the daemon uses logger
        use logger
        # the daemon should run after domainname
        after domainname
}

checkconfig() {
        if [ -z "$VAR_FROM_CONFIG" ] || [ -z "$VAR2_FROM_CONFIG" ] ; then
          eerror "You must set config options in /etc/conf.d/example first"
          return 1
        fi
}

start() {        
        # display to the user what you're doing
        ebegin "Starting Example daemon"  
        #run the checkconfig function
        checkconfig || return 1
        # Start the process as a daemon and record the pid number
        start-stop-daemon --start --background --pidfile /var/run/example.pid --make-pidfile --exec /usr/bin/example  
        # output success or failure
        eend $?
}

stop() {
        # display a message to the user
        ebegin "Stopping example daemon"
        # stop the daemon using the pid recorded from the start() function
        start-stop-daemon --stop --pidfile /var/run/example.pid --name example
        # output success or failure
        eend $?
}

This is the /etc/conf.d/example file

File: /etc/conf.d/example
#/etc/conf.d/example - configuration variables
VAR_FROM_CONFIG="EXAMPLE"
VAR2_FROM_CONFIG="EXAMPLE2"

If you need to pass arguments to the executable using the start-stop-daemon the start section of your rc-script will look like the following. In this example we'll be running /usr/bin/example -d

File: /etc/init.d/example
start() {
        # display to the user what you're doing
        ebegin "Starting Example daemon"  
        # Start the process as a daemon with the arguement "-d" and recording the pid number
        start-stop-daemon --start --background --pidfile /var/run/example.pid --make-pidfile --exec /usr/bin/example -- -d
        # output success or failure
        eend $?
}

If you have problems stopping your script after it's started, try using the following as an example:

File: /etc/init.d/example
#!/sbin/runscript

depend() {
        # the daemon needs the internet to function
        need net
}

start() {
        ebegin "Starting Example daemon"
        start-stop-daemon --start --exec /usr/bin/example
        eend $?
}

stop() {
        ebegin "Stopping Example daemon"
        start-stop-daemon --stop --exec /usr/sbin/example
        eend $?
}

Making Your Script Run

You may need to make the script executable before it will run.

user@machine $ chmod +x example

One Time

On Boot

Stopping The Script

See Also

Retrieved from "http://www.gentoo-wiki.info/Initscripts"

Last modified: Sat, 11 Oct 2008 00:48:00 +0000 Hits: 43,605