Info Summary[-][--][++]

Info
Runlevels are defined levels in which a linux operating system can start. Daemons (or services) are scripts/programs that run in the background constantly (like apache or mysql are services that are always running) and are usually executed at startup by the defined runlevel. Runlevels are identified by a number from 0 - 6, but each linux distribution may use those numbers a bit differently. Basically a runlevel determines which daemons to execute at startup. Runlevel 1 is typically known as single-user mode, it only executes the minimal set of scripts to get the system up and running (meaning you won't have xorg, mysql or apache... running at this level). This mode is often used as a recovery mode since most of your daemons are not running at this point. In many distros, the default runlevel is 3 or 5, this is the normal full mode, meaning all your normal services are started including your xorg graphical environment.

Each linux distribution is a bit different, even if it uses the standard System V, it may have additional helper scripts to start/stop/enable/disable services. Read below for distro specific daemon information.

System V[-][--][++]

Info
Many distros use a standard boot method, or a standard series of executed scripts, known as the "System V" (system five) init scripts. System V is just a script design and layout. When your system boots, it executes a series of scripts and then it starts your daemons. Daemon scripts are located in /etc/init.d. At certain runlevels, some of these daemons may or may not be started. Which services start at which runlevel is defined by the /etc/rc0.d/, /etc/rc1.d/, ... folders. So all daemons which will run at startup for runlevel 5 are in the /etc/rc5.d/ folder. The scripts in those runlevel folders are not the real scripts, but rather a symbolic link to the actual scripts in /etc/init.d/. The symbolic links in those folders start with a S if they are to be enabled, or a K if they are to be disabled and are followed by a number which is the order they will be executed. So /etc/rc2.d/S20mysql will start before /etc/rc2.d/S30apache and /etc/rc2.d/K20pure-ftpd is disabled. All services in the /etc/rcS.d/ folder are always executed first, at any runlevel, they can also be disbaled by renaming with a K instead of an S.

When you shutdown your computer, you are actually executing a runlevel (different per distro), usually 0 or 6. When you execute a shutdown or change a runlevel, any link starting with a K in that runlevel folder will be executed with the stop command. So you will notice all your services should be in the rc0.d or rc6.d folder with a K, that means on computer shutdown or reboot, stop all daemons nicely. Or you will notice many services are in the rc1.d folder as K. This is because runlevel 1 should only have a few daemons running, all others should be shutdown.

To manage which daemons execute on startup you can manually modify those symbolic links or you can use the helper scripts defined by your linux distro.

Managing services requires root privileges, so run as root or sudo

  • Show all available services with ls /etc/init.d
  • Start a service with /etc/init.d/myservice start
  • Stop a service with /etc/init.d/myservice stop
  • Show the status of a service with /etc/init.d/myservice statuts
  • Remove a service from all runlevels (will not start on boot) with rm /etc/rc*/*myservice
  • Find the current runlevel runlevel
  • Change runlevels telinit X where X can be 0-6 or S (S meaning run scripts in the /etc/rcS.d folder)

Ubuntu[-][--][++]

Info
Before 2006, ubuntu used the standard System V boot script method. Around 2006 ubuntu introduced what they call Upstart. Upstart is a new boot and daemon design. To maintain backward compatibility ubuntu created a System V legacy system which allows you to control ubuntu's deamons much like you would any other System V system. Ubuntu still has the /etc/init.d/ daemons script folder and all the runlevel symbolic links are in the corresponding /etc/rcXX.d folders, but they prefer you use their new Upstart method to manage your services. Since most services have been converted to upstart, you may find none of them appear in your rcXX.d folders, even K for shutdown on runlevel 0 or 6, thats OK, upstart manages their start and stop functionality via their config in /etc/init/myservice.conf

With Upstart, runlevels become obsolete because services are not executed at startup like the old System V, they are executed only if and when they are needed. You can read more about Ubuntu's System V and Upstart methods here https://help.ubuntu.com/community/UbuntuBootupHowto

In Ubuntu, you can still use the old legacy System V method they provide (see System V Above), or use their new Upstart methods:

Exclamation3
Ubuntu's update-rc.d helper script basically creates the /etc/rcXX.d/myservice symbolic links for you. If you add myservice to default runlevel with update-rc.d myservice defaults it determines the order (number 00-99) and creates the /etc/rcXX.d/Sxxmyservice links for you. It also creates the shutdown link in ALL rcXX.d folders (so Kxxmyservice). Anytime you manually add or remove a runlevel symbolic link, you should also run the update-rc.d theservice defaults script.

Managing services requires root privileges, so run as root or sudo

  • Show all available services with service --status-all
  • Show running services and their status with initcrl list
  • Start a service with service myservice start
  • Stop a service with service myservice stop
  • Show the status of a service with service myservice status

Remember that Upstart doesn't have runlevels, but for legacy, you can still start a service at boot

  • Add a service to the default runlevel (so service will start at boot or default runlevel) with update-rc.d myservice defaults (this creates the correct /etc/rcXX.d K and S symbolic links for you.
  • To add a service in a certian order use update-rc.d myservice defaults 98 02 will create the /etc/rcXX.d/S98myservice and /etc/rcXX.d/S02myservice scripts. So it will start late in the chain (98) and be killed very soon on a reboot or shutdown command (02).
  • Remove a service from all runlevels (will not start on boot) with rm /etc/rc*/*myservice or rename it with a K in front, then run update-rc.d myservice defaults

Service Managers (GUI and CLI)[-][--][++]

Services in Fedora Command Line[-][--][++]

http://linuxhelp.blogspot.com/2006/04/enabling-and-disabling-services-during_01.html

Red Hat and Red Hat based Linux distributions (like fedora, centos...) make use of the script called chkconfig to enable \
and disable the system services running in Linux.

For example, to enable the apache webserver to start in certain run levels,
you use the chkconfig script to enable it in the desired run levels as
follows:

# chkconfig httpd --add
# chkconfig httpd on --level 2,3,5

This will enable the apache webserver to automatically start in the run
levels 2, 3 and 5. You can check this by running the command:

# chkconfig --list httpdOne can also disable the service by using the off flag as shown below:
# chkconfig httpd off
# chkconfig httpd --del

Red Hat also has a useful script called service which can be used to start
or stop any service (this is real-time, where chkconfig on/off after reboot). Taking the previous example, to start apache
webserver, you execute the command:

# service httpd startand to stop the service...
# service httpd stopThe options being start, stop and restart which are self explanatory.

Here is the code for their (Fedora) sevice script

#!/bin/sh

. /etc/init.d/functions

VERSION="`basename $0` ver. 0.91"
USAGE="Usage: `basename $0` < option > | --status-all | \
[ service_name [ command | --full-restart ] ]"
SERVICE=
SERVICEDIR="/etc/init.d"
OPTIONS=

if [ $# -eq 0 ]; then
   echo "${USAGE}" >&2
   exit 1
fi

cd /
while [ $# -gt 0 ]; do
  case "${1}" in
    --help | -h | --h* )
       echo "${USAGE}" >&2
       exit 0
       ;;
    --version | -V )
       echo "${VERSION}" >&2
       exit 0
       ;;
    *)
       if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
          cd ${SERVICEDIR}
          for SERVICE in * ; do
            case "${SERVICE}" in
              functions | halt | killall | single| linuxconf| kudzu)
                  ;;
              *)
                if ! is_ignored_file "${SERVICE}" \
            && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
                  env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" status
                fi
                ;;
            esac
          done
          exit 0
       elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
          SERVICE="${1}"
          if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
            env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" stop
            env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" start
            exit $?
          fi
       elif [ -z "${SERVICE}" ]; then
         SERVICE="${1}"
       else
         OPTIONS="${OPTIONS} ${1}"
       fi
       shift
       ;;
   esac
done

if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
   env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" ${OPTIONS}
else
   echo $"${SERVICE}: unrecognized service" >&2
   exit 1
fi

Ubuntu/Debian[-][--][++]

Ubuntu also has chkconfig, but not in any repositories, though I did find an article here http://ubuntuforums.org/archive/index.php/t-20583.html which says you can install chkconfig with (untested)

$ apt-get install libnewt0.51
$ ln -s /usr/lib/libnewt.so.0.51 /usr/lib/libnewt.so.0.50
$ wget http://www.tuxx-home.at/projects/chkconfig-for-debian/chkconfig_1.2.24d-1_i386.deb
$ dpkg --force-all -i chkconfig_1.2.24d-1_i386.deb

Though a good alternative is sysv-rc-conf (I like it better because it gives a graphical'ish CLI)

$ apt-get install sysv-rc-conf
$ sysv-rc-conf --help
$ sysv-rc-conf --list
$ sysv-rc-conf --levels 35 servicename on
$ sysv-rc-conf (just this give a nice graphics (CLI) manager)

Another CLI one is rcconf

And you can get a nice service script by just do this: then typing 'service apache reload, or whatever'

# apt-get install sysvconfig

And of course you can also manage it yourself by managing the files in /etc/rcX.d
To disable a script, say in /etc/rc3.d, just rename it with a K in front, and take the number and subtract by 100.
So to disable webmin in runlevel 3, and mysql from runlevel 3

mv /etc/rc3.d/S99webmin /etc/rc3.d/K01webmin
mv /etc/rc3.d/S19mysql /etc/rc3.d/K81mysql

And visa versa to enable. Remember these script in /etc/rcX.d just point to all the scripts in /etc/init.d. Also \
NOTE: the subtracting 100 idea is only for scripts in rcX.d, NOT the scripts in rcS.d, just replace the S with a K for \
those scripts.

And remember that the folder /etc/rcS.d is run first, before any runlevel, this is knows as your startup scripts. \
after all these are run (in order by filename asc), then it loads up the script in the specified runlevel (note, \
if you are in runlevel 3, it only loads scripts for runlevel 3, not 1 then 2 then 3, just 3, which is why there are \
duplicate links in each /etc/rcX.d folder).

A nice note, from the ubuntu rcS.d README

The following sequence points are defined at this time:

* After the S40 scripts have executed, all local file systems are mounted
  and networking is available. All device drivers have been initialized.

* After the S60 scripts have executed, the system clock has been set, NFS
  filesystems have been mounted (unless the system depends on the automounter,
  which is started later) and the filesystems have been cleaned.

Graphics Service Managers[-][--][++]

BUM[-][--][++]

Ubuntu has its built in service manager, so does fedora. Another good package you can download is \
Boot Up Manager (bum).

# apt-get install bum

Very nice because it gives descriptions of each service, and lets you view and enable/disable your start scripts \
(not daemons, just scripts executed at startup). One bad thing, it limits what you can do, I noticed several \
services that were not listed (probably because they were very important ?**, best thing is manage yourself in init.d
.Screenshot-Boot-Up+Manager_preview.png

Test Performance[-][--][++]

A nice way to test boot performance is a program called Boot Chart. It starts just be fore any boot script, and ends \
after all other scripts, then generates a nice graphic of your systems performance.

# apt-get install bootchart

Autostart a daemon in Arch Linux[-][--][++]

edit the /etc/rc.conf and add to the DAEMONS=(...) they do start in order

Create a Daemon Script (not tested)[-][--][++]

#!/bin/sh
#
# myservice     This shell script takes care of starting and stopping
#               the 
#

# Source function library
. /etc/rc.d/init.d/functions

# Do preliminary checks here, if any
#### START of preliminary checks #########

##### END of preliminary checks #######

# Handle manual control parameters like start, stop, status, restart, etc.

case "$1" in
  start)
    # Start daemons.

    echo -n $"Starting  daemon: "
    echo
    daemon 
    echo
    ;;

  stop)
    # Stop daemons.
    echo -n $"Shutting down : "
    killproc 
    echo

    # Do clean-up works here like removing pid files from /var/run, etc.
    ;;
  status)
    status 

    ;;
  restart)
    $0 stop
    $0 start
    ;;

  *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit 0

Resources[-][--][++]

  1. http://www.linuxforums.org/forum/linux-newbie/97097-how-create-linux-service-daemon.html
FIX: add regular daemon control (/etc/init.d/), add archlinux, add program for upstart...