When you boot your system, a number of tasks need to be performed before you are
able to log on. This "normal" boot operation is fully defined -- it will be the
same every time you restart your system. A
The process that takes care of the runlevels is called
si::sysinit:/sbin/rc sysinit rc::bootwait:/sbin/rc boot
As you can see,
When the boot runlevel is completed (the boot runlevel is an intermediate
one),
id:3:initdefault:
In this case, the runlevel id is "3", and gets mapped to:
l3:3:wait:/sbin/rc default
In other words, the
When you take a look at
l0:0:wait:/sbin/rc shutdown l1:S1:wait:/sbin/rc single l2:2:wait:/sbin/rc nonetwork l3:3:wait:/sbin/rc default l4:4:wait:/sbin/rc default l5:5:wait:/sbin/rc default l6:6:wait:/sbin/rc reboot
As you can see, there is a mapping of numbers to names (not vice versa). For instance, 0 maps to "shutdown", 1 to "single" etc. Vice versa is not true, as "default" is used by 3, 4 and 5. These numbers are the runlevel numbers. Most distributions work with the numbers; Gentoo however decided to make it a bit more userfriendly and continue with the naming.
As you can see from the listings, Gentoo defines 7 runlevels. Three of them are
internal runlevels:
The other four runlevels are
If you take a closer look to
Each script in
To start, stop or restart a service (and all depending services),
# /etc/init.d/postfix start
If you want to stop a service, but not the services that depend on it, you can
use the
# /etc/init.d/postfix pause
If you want to see what status a service has (started, stopped, paused, ...) you
can use the
# /etc/init.d/postfix status
If the status information tells you that the service is running, but it doesn't,
then you can reset the status information to "stopped" with the
# /etc/init.d/postfix zap
To also ask what dependencies the service has, you can use
# /etc/init.d/postfix ineed
Similarly, you can ask what services require the service (
# /etc/init.d/postfix needsme
Finally, you can ask what dependencies the service requires but that are missing:
# /etc/init.d/postfix broken
Gentoo's init system uses a dependency-tree to decide what service needs to be started first. As this is a tedious task that we wouldn't want our users to do manually, we have created tools that ease the administration of the runlevels and init scripts.
With
You have already added init scripts to the "default" runlevel during the
installation of Gentoo. At that time you might not had a clue what the
"default" is for, but now you should. The
To add or remove an init script, just give
# rc-update del postfix default
The
# rc-update show
Init scripts can be quite complex. It is therefor not really interesting to have the users directly edit the init script, as it would make it more error-prone. It is however important to be able to configure such a service. For instance, you might want to give more options to the service itself.
A second reason to have this configuration outside the init script is to be able to update the init scripts without being afraid that your configuration changes are undone.
Gentoo provides an easy way to configure such a service: every init script that
can be configured has a file in
APACHE2_OPTS="-D PHP4"
Such a configuration file contains variables and variables alone (just like
No. Writing an init script is usually not necessary as Gentoo provides ready-to-use init scripts for all provided services. However, you might have installed a service without using Portage, in which case you will most likely have to create an init script.
Do not use the init script provided by the service if it isn't explicitly written for Gentoo: Gentoo's init scripts are not compatible with the init scripts used by other distributions!
The basic layout of an init script is shown below.
#!/sbin/runscript
depend() {
(Dependency information)
}
start() {
(Commands necessary to start the service)
}
stop() {
(Commands necessary to stop the service)
}
restart() {
(Commands necessary to restart the service)
}
Any init script
There are two dependencies you can define:
A
Let us take a look at the dependency information for the postfix service.
depend() {
need net
use logger dns
provide mta
}
As you can see, the postfix service:
In some cases you might not require a service, but want your service to be
started
As an example we view the settings of the Portmap service:
depend() {
need net
before inetd
before xinetd
}
You can also use the "*" glob to catch all services in the same runlevel, although this isn't adviseable.
depend() {
before *
}
Next to the
start() {
ebegin "Starting my_service"
start-stop-daemon --start --quiet --exec /path/to/my_service
eend $?
}
If you need more examples of the
# man start-stop-daemon
Other functions you can define are:
If you want your init script to support more options than the ones we have
already encountered, you should add the option to the
opts="${opts} restartdelay"
restartdelay() {
stop()
sleep 3 # Wait 3 seconds before starting again
start()
}
You don't have to do anything to support a configuration file in
Also, if your init script provides a virtual dependency (such as