runlevels are an operating mode a linux os runs in. there are usually 7 different runlevels
0 - halt, system shutdown 1 - single user mode, for administrative stuff, used if you want to reset the root password after you forgot it 2 - multi-user mode, like 'normal' mode, but without any network services running 3 - multi-user mode with networking, the normal operating mode of linux operating systems 4 - custom, a user defined mode that generally isn't used 5 - display manager, run level 3 plus a display manager 6 - reboot, does a restart of the operating system
how does the system know for each run level, what to do?
inside /etc/ you will see a bunch of directories named rc#.d like this
$ sudo ls -l /etc/rc* drwxr-xr-x. 2 root root 4096 Oct 13 03:41 init.d -rwxr-xr-x. 1 root root 2617 Aug 13 23:58 rc drwxr-xr-x. 2 root root 4096 Oct 13 03:41 rc0.d drwxr-xr-x. 2 root root 4096 Oct 13 03:41 rc1.d drwxr-xr-x. 2 root root 4096 Oct 13 03:44 rc2.d drwxr-xr-x. 2 root root 4096 Oct 13 03:44 rc3.d drwxr-xr-x. 2 root root 4096 Oct 13 03:44 rc4.d drwxr-xr-x. 2 root root 4096 Oct 13 03:44 rc5.d drwxr-xr-x. 2 root root 4096 Oct 13 03:41 rc6.d -rwxr-xr-x. 1 root root 220 Aug 13 23:58 rc.local -rwxr-xr-x. 1 root root 19841 Aug 13 23:58 rc.sysinit
these directories contain scripts the system uses at startup and shutdown to know in which order to start and stop applications and utilities for each of the 7 run levels.
run level 3 might have stuff like this
$ sudo ls -l /etc/rc3.d/ K01smartd -> ../init.d/smartd K09lwsmd -> ../init.d/lwsmd K10cups -> ../init.d/cups K10psacct -> ../init.d/psacct K10saslauthd -> ../init.d/saslauthd K15htcacheclean -> ../init.d/htcacheclean . . . K87rpcbind -> ../init.d/rpcbind K89rdisc -> ../init.d/rdisc K99rngd -> ../init.d/rngd S01sysstat -> ../init.d/sysstat S02lvm2-monitor -> ../init.d/lvm2-monitor S07iscsid -> ../init.d/iscsid S08ip6tables -> ../init.d/ip6tables S08iptables -> ../init.d/iptables S10network -> ../init.d/network . . . lrwxrwxrwx. 1 root root 15 Mar 24 2015 S97rhnsd -> ../init.d/rhnsd lrwxrwxrwx. 1 root root 19 Mar 24 2015 S97rhsmcertd -> ../init.d/rhsmcertd lrwxrwxrwx. 1 root root 19 Mar 30 2015 S98pe-puppet -> ../init.d/pe-puppet lrwxrwxrwx. 1 root root 11 Oct 9 17:04 S99local -> ../rc.local
these represent the kill order and start order for each run level. for startup, the scripts are executed in the order of S plus the lowest numbers in an alphabetical order, not numeric, so it would go something like
S01 -> S12 -> S1
and the kill order is the same
K01 -> K99
important systems services are started early on boot, and on shutdown are done towards the end, like a first in last out sequence.
when you use the chkconfig utility to add a service to start on boot or something, chkconfig looks inside /etc/init.d/yourservice in the comments at the top, for the line that specifies which runlevels and start order and kill order to apply, then it goes and sets up the links in these directories
#!/bin/sh # # supervisord Startup script for supervisord # # chkconfig: 2345 85 15 # processname: supervisord
in the snippet above on the chkconfig line, 2345 are the run levels, 85 is the start order, so quite late, and 15 is the kill order, so quite early
you can’t create an init.d script without this information at the top, as it’s important for the startup and shutdown sequences of the host
if you want to change runlevels, you can do something like this
$ sudo init 6