When you boot up a Linux computer, you'll usually see the bootloader (after the BIOS has done its part of the booting process). The bootloader, usually Lilo or GRUB, is configured to load the kernel of the Linux into the memory of your computer. The kernel then leaves the bootloader behind and continues booting on its own, initializing hardware and making itself ready to start running init. And init is the process we're looking for: it is the first process run, and it spawns all other processes. But where does init look to know which processes to start? The short answer is: in /etc/rc*.d/ (usually). But that's not the full answer. To understand how it uses the files in those directories, we will first need to understand what runlevels are.
Runlevels
A runlevel is the process state in which a system can reside. For example: runlevel 1 usually is the rescue mode, it boots the system starting as few processes as possible, providing the user with a root-access console, without networking functionality and without any GUI. On the other hand, runlevel 5 usually is the full-fledged graphical multi-user mode. There also are special runlevels that you shouldn't boot to, but that you could switch to after booting: for example, switching to runlevel 0 (zero) shuts down your computer (don't try this until you read the full tutorial) and switching to runlevel 6 will make your system reboot itself. Now for the full list of runlevels and their common configuration:
* 0: Shuts down the system (to be switched to after booting)
* 1: Boots the system to a rescue mode
* 2: Boots the system to a multi-user mode without networking functionality (some systems, like Debian, use this as a full-fledged mode)
* 3: Boots the system to a multi-user mode with networking functionality (some systems, like Debian, use this as a full-fledged mode)
* 4: Generally not used (sometimes it is the same as runlevel 5 or 3)
* 5: Boots the system to a full-fledged graphical multi-user mode (with networking, of course)
* 6: Reboots the system (to be switched to after booting)
* (s or S: Usually similar or equal to runlevel 1)
As the last runlevel is not always considered a real runlevel (it is then called an alias), the number of runlevels is usually seven, but arguably eight. You can switch between all runlevels after having booted, although 0 and 6 will (of course) require you to boot again before you can switch again. Switching between runlevels is usually done with the init command, like this:
[rechosen@localhost ~]$ init 3
The above command would (run as root or with root permissions) switch your system to runlevel 3, unless it's already running in runlevel 3 (replace the 3 with an other digit if you want to switch to an other runlevel). When switching between runlevels, init terminates the processes that were running in the old runlevel which should not run in the new one (these are zero processes when booting and all processes when shutting down or rebooting) and starts the processes that were not running in the old runlevel which should run in the new runlevel (these are zero processes when shutting down or rebooting, but when booting, it depends on the runlevel you're booting to). And now we can go on to the way init "knows" what to start and what to stop.
What to start and what to stop?
When the init process is started by the kernel, it first looks in the /etc/inittab file to see what to do in case of a certain runlevel. This file usually tells init to look in the corresponding /etc/rcX.d/ directory, where X is the runlevel. For example, when booting to runlevel 5, init will run the scripts in /etc/rc5.d/ (please note (again) that this does not apply to all distros). These "scripts" usually are symlinks to the corresponding service management scripts in /etc/init.d/ (or /etc/rc.d/init.d/). But how can such a script understand whether init wants it to start or stop the service? Well, when init wants the script to start the process, it will run the script with "start" as the first argument. When wanting the script to stop the service, init will pass "stop" as the first argument. You can also do this yourself if you want to start or stop a service. For example:
[rechosen@localhost ~]$ /etc/init.d/ntpd stop
The above example would stop the ntpd daemon. To start it again use the following command:
[rechosen@localhost ~]$ /etc/init.d/ntpd start
Stopping and starting can usually be combined in the following way.
[rechosen@localhost ~]$ /etc/init.d/ntpd restart
This is useful in case you want a daemon to re-read its config files. Depending on the service the script was written for (and the script itself), other arguments may also be supplied.
Anyway, let's get back to the /etc/rcX.d/ directory. This directory is filled with files named like "K59somedaemon" and "S10anotherdaemon". I'll give a short explanation:
* "K" means "kill" and "S" means "start": The symlinks starting with "K" will be executed with a "stop" argument by init, and the symlinks starting with "S" will be executed with a "start" argument.
* The two digits after "K" or "S" don't mean anything really special, they are just used for sorting. Symlinks that need to be run before certain other ones should have a lower number than the other ones.
* The name of the daemon after it is actually not necessary for init, but it's good practice for sysadmins so they can instantly see what the symlink is for.
Finally, you should know that pretty much any distro has got some check somewhere to ensure that already running processes are not started again (which would result into two interferring processes, unless the daemon itself checks if there isn't an other instance running) and that already terminated processes will not be "reterminated". This is usually done by some special program or script that is called to do the actual starting.
分享到:
相关推荐
Linux中文HOWTO Linux中文HOWTO Linux中文HOWTO
这个是合集~一般是这里最全的了 另外加的 PDF是英文的 .为 GCC4.1的HOWTO <br>目录 <br>ADSM-Backup-html<br>Advocacy<br>Automount-html<br>Battery-Powered-html<br>BootPrompt-HOWTO<br>Bridge-......
With its combination of background, theory, real-world examples, and patient explanations, How Linux Works will teach you what you need to know to solve pesky problems and take control of your ...
《Chinese-Linux-HOWTO》是一份详细的指南,专注于在中文环境下配置和使用Linux操作系统。这份文档深入到源码级别,旨在帮助用户理解和解决在安装、配置和使用中文Linux时遇到的各种问题。它不仅适用于初学者,也对...
Linux HOWTO(中文版) BootPrompt-HOWTO (28KB) 启动提示说明 CDROM-HOWTO (27KB) 如何安装, 设定及使用光驱,同时列出支援的硬体. Chinese-HOWTO (39KB) 如何在 Linux 的系统上使用中文?/TD> Config-...
Linux Howto 中文版完整版.zip
how Linux work 精通Linux 中文第二版 带书签,纯文字版,非扫描 Brian Ward
Linux HOWTO 中文版(全) Linux HOWTO 中文版(全) Linux HOWTO 中文版(全)
how linux work
Linux HOWTO 中文版是一份全面的指南,旨在帮助初学者和有经验的用户更好地理解和操作Linux操作系统。这份文档详细地介绍了Linux系统的基础知识、安装过程、常用命令、系统管理以及一些高级主题。以下是这份资源中...
1-5974490210), Stealing the Network: How to Own an Identity (ISBN: 1597490067), InfoSec Career Hacking (ISBN: 1597490113), and OS X for Hackers at Heart (ISBN: 1597490407). He has a BS from Angelo ...
《精通Linux》英文原书,第二版
How Linux Works 英文版PDF文字版 2nd Edition what Every superuser should know by Brian Ward San Francisco 介绍linux使用
在“Linux DRM how-to”中,你可能会学习到如何配置DRM系统,编写libdrm驱动程序,调试图形问题,以及如何利用现有的开源工具进行测试。理解并掌握这些知识点对于从事Linux图形栈开发或系统优化的工程师来说至关重要...
How Linux Works-what every superuser should know。
"Linux How To"通常是指一系列针对Linux操作系统的教程或指南,旨在帮助用户更好地理解和使用这个平台。这里列出的压缩包文件包含了多个主题的HOWTO文档,都是关于Linux特定领域的详细指南。 1. **PPP-HOWTO-...
How to be efficient at the command line by using aliases, tab completion, and your shell history. How to schedule and automate jobs using cron. How to switch users and run processes as others. ...
Linux "how-to"手册是一系列详细的指南,专为Linux用户和管理员提供操作步骤和技术说明。这些手册涵盖了Linux系统中的各种主题,旨在帮助用户理解和解决特定问题。以下是对压缩包文件中部分子文件名称的解释,以及...