A Little About Linux: Changing Runlevels, Shutting Down or Rebooting

Runlevels are operation modes that appear on any Unix-like System-V based Operating System such as Linux (more about System-V later.)  There are seven runlevels numbered 0-6 each of which has a designated set of services or system configurations when the system is booted in that mode.  This whole section may seem strange and archaic for anyone coming from a Microsoft Windows background, but stick with me as it it really isn't that hard.  Firstly, the list of runlevels and their functions are;

0 - Shutdown or Halt the system.
1 - Single User Mode.  Only really used if there is a problem or if the system needs to be in maintenance.  Aliased as 's' or 'S'.
2 - Debian based systems use this runlevel as a multi-user environment with a full X Windows GUI.  It's undefined in other systems or maybe used but may not have NFS enabled. 
3 - Red Hat (and most other distros) use this run level as a multi-user environment but with no GUI i.e. Command Line only.
4 - User-definable.
5Red Hat (and most other distros) use this run level as a multi-user environment with a full X Windows GUI.
6 - Reboot the system.

Apart from Debian based systems (including Unbuntu and Mint), the Red Hat runlevels are usually the norm throughout all other distros.

The initial default runlevel is set in /etc/inittab, this will be shown as; id:3:initdefault: (if the default level is a multi-user system with no GUI.)

As you can see, the format is; id:runlevels:action:process

id - A unique sequence of 1-4 characters which identifies an entry in inittab
runlevels - Lists the runlevels for which the specified action should be taken.
action - Describes which action should be taken.
process - Specifies the process to be executed



/etc/event.d/ is now used by some systems to control the init startup sequence.
System-V Startup Scripts
System-V (or SysV) was an early Unix system back in the 1980s, a lot of standards were written down at this time and some, like the System-V inter-process communication mechanism, are still available in Linux today.  The SysV init runlevel system provides a standard process for controlling which programs the init command launches or halts when initialising a runlevel.  

The script /etc/rc.d/rc is responsible for starting/stopping services when the runlevel changes and runs all scripts associated with that runlevel.  These scripts are stored in;

/etc/rc0.d  
/etc/rc1.d  
/etc/rc2.d  
/etc/rc3.d  
/etc/rc4.d  
/etc/rc5.d  
/etc/rc6.d

The number in the directory name equqtes to the runlevel and in each of these directories, there is a symbolic link to where the actual start/stop script exists (this is usually in /etc/init.d/.)  Each one of these symlinks is given the name of the software it calls, prefixed by a letter S or K and a number.

S = The symlink is set to start the script whenever the specific runlevel shown in the directory name is entered.
K = The symlink is set to stop (kill) the script whenever the specific runlevel shown in the directory name is exited.
number = The order in which it is started or stopped.  The lower the number, the earlier its started.

So, now to put all of this into an example.  Let's examine an installed application and how it's set to run... in this case we'll look at Apache HTTPd.

Looking in /etc/init.d/ there should be a script called 'httpd', which controls the start up, stopping, config reloading and other functionality of Apache httpd.  

Now, looking at the contents of the /etc/rcX.d directories (its probably easier if a command like  ls -lha rc*/*httpd is used) the results show;

rc0.d/K15httpd -> ../init.d/httpd
rc1.d/K15httpd -> ../init.d/httpd
rc2.d/S85httpd -> ../init.d/httpd
rc3.d/S85httpd -> ../init.d/httpd
rc4.d/S85httpd -> ../init.d/httpd
rc5.d/S85httpd -> ../init.d/httpd
rc6.d/K15httpd -> ../init.d/httpd

From the output above, we can see that there is a symlink in every directory pointing back to the /etc/init.d/httpd script and the name of the symlink tells the whole story;

rc0.d/K15httpd - runlevel 0, stops httpd fairly early on in the shutdown process. 
rc1.d/K15httpdrunlevel 1, stops httpd fairly early on in the shutdown process.
rc2.d/S85httpdrunlevel 2, starts httpd late on in the startup process. 
rc3.d/S85httpd - runlevel 3, starts httpd late on in the startup process. 
rc4.d/S85httpd - runlevel 4, starts httpd late on in the startup process. 
rc5.d/S85httpd - runlevel 5, starts httpd late on in the startup process. 
rc6.d/K15httpd - runlevel 6, stops httpd fairly early on in the shutdown process.

So, you're all starting to think, "blimey this seems complicated, is there any other way to do all this?" and the good news is that there are tools to help you.

Tools for Managing Runlevel Services
The two tools that are common are chkconfig and ntsysv.

With chkconfig you can show all of the runlevels for all installed services...

# chkconfig --list

auditd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off
haldaemon       0:off   1:off   2:off   3:on    4:on    5:on    6:off
htcacheclean    0:off   1:off   2:off   3:off   4:off   5:off   6:off
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
ip6tables       0:off   1:off   2:on    3:on    4:on    5:on    6:off
iptables        0:off   1:off   2:on    3:on    4:on    5:on    6:off
lvm2-monitor    0:off   1:on    2:on    3:on    4:on    5:on    6:off
messagebus      0:off   1:off   2:on    3:on    4:on    5:on    6:off
minidlna        0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
netfs           0:off   1:off   2:off   3:on    4:on    5:on    6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
nmb             0:off   1:off   2:on    3:on    4:on    5:on    6:off
openvpn         0:off   1:off   2:on    3:on    4:on    5:on    6:off
pcscd           0:off   1:off   2:off   3:on    4:on    5:on    6:off
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off
rdisc           0:off   1:off   2:off   3:off   4:off   5:off   6:off
restorecond     0:off   1:off   2:off   3:off   4:off   5:off   6:off
rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off
saslauthd       0:off   1:off   2:off   3:off   4:off   5:off   6:off
smb             0:off   1:off   2:on    3:on    4:on    5:on    6:off
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
udev-post       0:off   1:on    2:on    3:on    4:on    5:on    6:off
winbind         0:off   1:off   2:off   3:off   4:off   5:off   6:off

Or just the one you're interested in...

# chkconfig --list httpd

httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

You can change what runlevels the application operates on by turning them off...


chkconfig --level 2345 httpd off
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off

Or back on...

# chkconfig --level 2345 httpd on

httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Now, if you've installed an application via a RPM or DEB package, chances are that there will already be a start/stop script located in /etc/init.d/ but if you've compiled it yourself, you'll need to register your own script as a service using the following...

chkconfig --add myapp

The other useful tool for changing runlevels is ntsysv (although I must be honest and say that I've never actually used it) and this is a TUI tool (Text-based GUI) so if you want to see what services are started on runlevels 12345 run the following and check the app you want to start or stop...

ntsysv --level 12345

What Runlevel am I currently on?
Checking the the current runlevel is easy, just run the following...

# runlevel

N 3

The first character is the last runlevel ('N' means none) and the second character is the current runlevel.

If you want to find the default runlevel, try the following...

# less /etc/inittab

id:3:initdefault:

How do I Change Runlevel?
In all honesty, you probably won't want to change the current runlevel often, apart from when you reboot or shutdown the machine, but if you need to change it use...

# init 5

or

# telinit 5

These will change the runlevel to an environment with full X Windows GUI, or to reload the value in /etc/inittab run...

# telinit q

Finally, if you need to switch to single-user/maintenance mode whilst booting up, I suggest you look at this tutorial on the Nixcraft site.

Stopping and Rebooting
Although the init or telinit commands can stop and reboot the machine, it's usually better to use some of the following commands to do these operation as they are more user friendly and allow the commands to be deferred or a message to be displayed to all users...

# shutdown -h now or # halt        - Halt the system immediately
# shutdown -r now or # reboot  - Reboot the system immediately

To delay these operations, a time can be added after the shutdown operation in place of the 'now' parameter.  '+m' is the amount of time in minutes, 'hh:mm' is the time in 24 hour format...

# shutdown -h +30       - Shutdown the machine in 30 minutes
# shutdown -r 21:00  - Reboot the machine at 9 pm 

You can also tell logged on users what you intend to do at 6.30...

# shutdown -r 18:30 "Server going down for maintenance at 6:30pm, please log off before then."

Finally, if you've scheduled an operation and want to cancel it before it happens, try this...

# shutdown -c

No comments: