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.
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
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:
Remember that Upstart doesn't have runlevels, but for legacy, you can still start a service at boot
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 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.
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
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
edit the /etc/rc.conf and add to the DAEMONS=(...) they do start in order
#!/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
FIX: add regular daemon control (/etc/init.d/), add archlinux, add program for upstart...