Basic
Runlevel
Runlevels may vary from OS to OS, but there're still some rules for them.
Standard Runlevels
0 JNB Shuts down the system
S Single-User Mode Does not configure network interfaces or start daemons
6 Reboot Reboots the system
LSB specifications
0 Halt Shuts down the system
1 Single-User Mode Mode for administrative tasks
2 Multi-User Mode Does not configure network and does not export network services
3 Multi-User Mode with Networking Starts the system Normally
4 Not used/User-definable For special purpose
5 Normal Start with a GUI runlevel 3 + display manager
6 Reboot Reboots the system
Files in rc${runlevel}.d have names like K[0-9][0-9]* and S[0-9][0-9]*, is this a naming rule?
Do the names have implications?
The answer for both are YES.
rc stands for 'runtime configuration'.
The name rule is <K or S><priority><service>.
K stands for 'Kill', which means this service should be killed once the system enters this runlevel.
S stands for 'Start', which means this service should be started once the system enters this runlevel.
The lower the priority number, the higher the priority.
An experience formula: priority-with-S + priority-with-K = 100
Code
#!/usr/bin/env python
# This program examines services under /etc/rc<Runlevel>.d
# and generate a summary report.
class Service:
def __init__(self, name):
self.name = name
self.start_rl_list = []
self.stop_rl_list = []
def getName(self):
return self.name
def addStartRL(self, rl):
self.start_rl_list.append(rl)
return
def addStopRL(self, rl):
self.stop_rl_list.append(rl)
return
def show(self):
print '<%s>' % self.name
print 'Start: ', self.start_rl_list
print 'Stop: ', self.stop_rl_list
print
return
import re
import sys, os
dirpath_pattern = re.compile('/etc/rc([0-6S]).d')
service_pattern = re.compile('([KS])[0-9][0-9](.*)')
service_list = []
def alter_service_list(service_list, service_name, runlevel, action):
for s in service_list:
if s.getName() == service_name:
if action == 'S':
s.addStartRL(runlevel)
else:
s.addStopRL(runlevel)
return
service = Service(service_name)
if action == 'S':
service.addStartRL(runlevel)
else:
service.addStopRL(runlevel)
service_list.append(service)
return
# traverse /etc, for every dir matching dirname_pattern
# examine every file in it, if file name matches service_pattern
# then extract its behavior group(1) and its name group(2)
# operate on the service list
DIRECTORY = '/etc'
for dirpath, dirnames, filenames in os.walk(DIRECTORY):
match = dirpath_pattern.match(dirpath)
if match:
runlevel = match.group(1)
for f in filenames:
match = service_pattern.match(f)
if match:
action = match.group(1)
service_name = match.group(2)
alter_service_list(service_list, service_name, runlevel, action)
for s in service_list:
s.show()
Result
chenqi@chenqi-OptiPlex-760:~/mypro/python/examine-service$ ./examine_service.py | awk 'BEGIN { FS="\n"; RS=""; OFS="\t\t\t\t" } { print $1, $2, $3 }'
<pulseaudio> Start: ['5', '2', '3', '4'] Stop: ['1']
<saned> Start: ['5', '2', '3', '4'] Stop: ['1']
<killprocs> Start: ['1'] Stop: []
<acpi-support> Start: ['5', '2', '3', '4'] Stop: ['1']
<apache2> Start: ['5', '2', '3', '4'] Stop: ['1', '0', '6']
<dns-clean> Start: ['1', '5', '2', '3', '4'] Stop: []
<speech-dispatcher> Start: ['5', '2', '3', '4'] Stop: ['1', '0', '6']
<single> Start: ['1'] Stop: []
<pppd-dns> Start: ['1', '5', '2', '3', '4'] Stop: []
<kerneloops> Start: ['5', '2', '3', '4'] Stop: ['1']
<grub-common> Start: ['5', '2', '3', '4'] Stop: []
<ondemand> Start: ['5', '2', '3', '4'] Stop: []
<rc.local> Start: ['5', '2', '3', '4'] Stop: []
<sudo> Start: ['5', '2', '3', '4'] Stop: []
<rsync> Start: ['5', '2', '3', '4'] Stop: []
<apparmor> Start: ['S'] Stop: []
<setserial> Start: ['S'] Stop: ['0', '6']
<x11-common> Start: ['S'] Stop: []
<etc-setserial> Start: ['S'] Stop: ['0', '6']
<brltty> Start: ['S'] Stop: []
<urandom> Start: ['S', '0', '6'] Stop: []
<halt> Start: ['0'] Stop: []
<umountnfs.sh> Start: ['0', '6'] Stop: []
<sendsigs> Start: ['0', '6'] Stop: []
<umountfs> Start: ['0', '6'] Stop: []
<umountroot> Start: ['0', '6'] Stop: []
<networking> Start: ['0', '6'] Stop: []
<unattended-upgrades> Start: [] Stop: ['0', '6']
<reboot> Start: ['6'] Stop: []
chenqi@chenqi-OptiPlex-760:~/mypro/python/examine-service$ ./examine_service.py | awk 'BEGIN { FS="\n"; RS=""; OFS="\t\t\t\t" } { print $1, $2, $3 }' | grep apache
<apache2> Start: ['5', '2', '3', '4'] Stop: ['1', '0', '6']