In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail the example analysis of running level and restart in Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Overview
In this article, learn to shut down or restart your Linux system to warn users that the system is shutting down. Switch to single-user mode or a somewhat restrictive runlevel. Learning:
Set the default run level
Changes between run levels
Change to single-user mode
Shut down or restart the system from the command line
Warn users of major system events, including switching to another runlevel
Terminate the process appropriately
Unless otherwise noted, the examples in this article use Fedora 8 with a 2.6.26 kernel. The upstar example uses Fedora 13 with a 2.6.34 kernel, or Ubuntu 10.10 with a 2.6.35 kernel. Your results may vary on other systems.
precondition
To make the most effective use of the articles in this series, you should have a basic knowledge of Linux and need to prepare a Linux system to practice the commands introduced in this article. Sometimes different versions of the program have different output formats, so the results you get may not always be the same as the listings and diagrams shown here. In particular, recent systems have changed what many traditional System V init process users are familiar with (see Init vs. Upstart for more details). This article is aimed at the traditional System V init process and only gives a rough overview of the differences between upstar.
Operation level
The run level defines the tasks that can be accomplished in the current state (or run level) of the Linux system. Each Linux system supports three basic runlevels, plus one or more runlevels for normal operation. The basic run level is shown in Table 1.
Table 1. Linux basic runlevel level
Purpose
0 shut down (or terminate) the system 1 single-user mode: commonly referred to as s or S6 restart the system
In addition to these basic runlevels, runlevel usage varies from release to release. A command uses collections as shown in Table 2.
Table 2. Other commands linux run level
Level
Purpose 2
Multi-user mode without network
three
Multi-user mode with network
five
Multi-user mode with network and X Window System
The Slackware release uses runlevel 4 instead of 5 because the entire system is running the X Window system. Debian and its derivatives, such as Ubuntu, use a single run level, usually run level 2, for any multi-user mode. Be sure to consult your version of the documentation.
Default run level
When a Linux system starts, the default run level is determined by the id: entry under / etc/inittab. Listing 1 illustrates a typical entry for a system, such as Fedora 8 or openSUSE 11.2, both of which use runlevel 5 for X Window System.
Listing 1. Default run level in / etc/inittab
[root@pinguino ~] # grep "^ id:" / etc/inittabid:5:initdefault:
If you want your system to start at a different runlevel, such as runlevel 3, edit this value.
Change the running level
There are several ways to change the run level. To make a permanent change, you can edit / etc/inittab and change the default run level, as you just saw above.
If you only need to upgrade your system to a different runlevel in a single boot, you can do so. For example, suppose you just installed a new kernel and you need to build some kernel modules after rebooting the system with the new kernel, but before starting X Window System. You may want to upgrade your run level to level 3 to accomplish this task. You can do this at boot time by editing the kernel line (GRUB) or by adding a parameter after the selected system name (LILO). Use a single digit to indicate the desired run level (in this case, 3). We will introduce the process through an example of GRUB. Suppose your / boot/grub/menu.lst file contains the snippet shown in listing 2.
Listing 2. Launch a typical GRUB fragment of Fedora 8
Title Fedora (2.6.26.8-57.fc8) root (hd0,5) kernel / boot/vmlinuz-2.6.26.8-57.fc8 ro root=LABEL=FEDORA8 rhgb quietinitrd / boot/initrd-2.6.26.8-57.fc8.img
To upgrade the system to runlevel 3, wait until the boot entry appears, select the entry and type'e' to edit the entry. Depending on your GRUB option, you may need to press a key to display the boot entry, and then enter a'p 'and a password to unlock the edit as well. The GRUB screen on the Fedora 8 system looks like figure 1.
Figure 1. Select a boot option in GRUB
In this example, you should now see lines that start with root, kernel, and initrd. Move the cursor to the line that starts with "kernel" and type'e' to edit the line. The GRUB screen on the Fedora 8 system is now shown in figure 2.
Figure 2. Select kernel items for editing
Finally, move the cursor to the end of the line and add a space and the number'3'. You can delete 'quiet', or modify any other parameters as needed. The GRUB screen on the Fedora 8 system now looks like figure 3.
Figure 3. Set the startup run level to 3
Finally, press the Enter key to save the changes, and then type'b'to boot the system.
Note: the steps for using LILO or GRUB2 and using GRUB are different, but the basic principle of editing the kernel boot method is the same, even though the GRUB interface on other systems or versions is quite different from that shown here. Tips are usually helpful to you.
When you have completed your installation under runlevel 3, you may want to switch to runlevel 5. Fortunately, you do not need to restart the system. You can use the telinit command to switch to another runlevel. Use the runlevel command to display previous and current runlevels. If one of the output characters is' Nconfigured, the runlevel cannot be changed because the system is being booted. Listing 3 shows validation and changing the run level.
Listing 3. Verify and change the run level
[root@pinguino ~] # runlevelN 3 [root@pinguino ~] # telinit 5
After typing telinit 5, you will see several messages flash by, and your interface will switch to the configured graphical login interface. Open a terminal window and verify that the run level has been modified, as shown in listing 4.
Listing 4. Confirm the new run level
[root@pinguino ~] # runlevel3 5
If you use the ls command to display a long list of telinit commands, you will see that it is actually a symbolic link to the init command. We will explain this in listing 5.
Listing 5. Telinit is just a symbolic link to init
[root@pinguino] # ls-l $(which telinit) lrwxrwxrwx 1 root root 4 2008-04-01 07:50 / sbin/telinit-> init
The executable init knows whether it is called init or telinit, and then adjusts accordingly. Because init runs as PID 1 at boot time, it's also smart to know when you'll call it later using init instead of telinit. If you do, it assumes that you want it to behave as if it had already called telinit. For example, you might use init 5 instead of telinit 5 to switch to runlevel 5.
Single user mode
Compared to a PC operating system, such as DOS or Windows,Linux itself, it is a multi-user system. However, sometimes this can be a problem, for example, when you need to restore an important file system or database, or when you install and test some new software. Runlevel 1, single-user mode is your best answer in these situations. The actual implementation varies from version to version, but you usually simply boot with a small system. There is usually no networking and no (or very few) daemons running. On some systems, you must log in to authenticate, but on others you can start manipulating the shell prompt directly as the root user. Single-user mode can be a lifebuoy or damage to your system, so you should be careful whenever you use root privileges. Restart to a normal multi-user mode immediately after completion.
Just like switching to the normal multi-user runlevel, you can also use telinit 1 to switch to single-user mode. As described in Table 1,'s' and'S' are aliases for runlevel 1, so you can use telinit s instead.
Clean shutdown
You can use telinit or init to stop multi-user activity and then switch to single-user mode, which can be a bit sudden and can cause users to lose jobs and process terminations abnormally. The preferred way to shut down or restart the system is to use the shutdown command, which first sends a warning to all logged-in users and restricts further login, and then sends an init signal to switch runlevels. The init process then sends a SIGTERM signal to all running processes, giving them time to save the data or let them end the process themselves. After 5 seconds (or other delay time, if specified), init sends a SIGKILL signal to force the process that is still running to end.
By default, shutdown switches to runlevel 1 (single-user mode). You can specify-h to pause the system, or the-r option to restart. In addition to the message you specified, the standard message is also published. Time may be specified as absolute time, in hh:mm format, or it may be relative time, in n format, where n is the number of minutes from shutdown. For immediate shutdown, use now, which is equivalent to + 0.
If you have issued a delayed shutdown and the time is not yet up, if you want to cancel the shutdown, press Ctrl-c if the command line is running at the front end, or cancel the upcoming shutdown by issuing a shutdown command with the-c option. Listing 6 shows several examples of using the shutdown command and how to undo it.
Listing 6. Shutdown exampl
[root@pinguino ~] # shutdown 5 File system recovery neededBroadcast message from root (pts/1) (Tue Jan 4 08:05:24 2011): File system recovery neededThe system is going DOWN to maintenance mode in 5 minutes! ^ CShutdown cancelled. [root@pinguino ~] # shutdown-r 10 Reloading updated kernel& [1] 18784 [root@pinguino] # Broadcast message from root (pts/1) (Tue Jan 4 08:05:53 2011): Reloading updated kernelThe system is going DOWN for reboot in 10 minutes! [root@pinguino] # Fgshutdown-r 10 Reloading updated Kernel ^ CShutdown cancelled. [root@pinguino ~] # shutdown-h 23 shutdown 59 & [1] 18788 [root@pinguino ~] # shutdown-cShutdown cancelled. [1] + Done shutdown-h 23:59
You may have noticed that our last example did not send a warning message. If the shutdown time exceeds 15 minutes, this message will not be sent until 15 minutes before the activity shown in listing 7. Listing 7 also shows the use of the-t option, which increases the default delay between SIGTERM and SIGKILL signals by 5 to 60 seconds.
Listing 7. Another example of shutdown
[root@pinguino ~] # date;shutdown-t60 17 Time to do backups&Tue Jan 4 08:12:55 EST 2011 [1] 18825 [root@pinguino] # dateTue Jan 4 08:14:13 EST 2011 [root@pinguino] # Broadcast message from root (pts/1) (Tue Jan 4 08:14:55 2011): Time to do backupsThe system is going DOWN to maintenance mode in 15 minutes!
If you cancel a shutdown, you can use the wall command to send a warning to all users that the system is not actually about to shut down.
As we explained earlier, you can also use telinit (or init) to shut down or restart the system. As with other uses of telinit, no warning message is sent to the user, and the command takes effect immediately, although there is still a delay between the SIGTERM and SIGKILL signals. For other options for telinit, init, and shutdown, refer to the man pages.
Halt, reboot, and poweroff commands
You already know some commands about shutdown and reboot.
The halt command pauses the system.
The poweroff command is a symbolic link to the halt command that pauses the system and then attempts to power off.
The reboot command is another symbolic link to the halt command, pause the system and then restart.
When the running level of the system is not 0 or 6, if one of these commands is called, the corresponding shutdown command will be called instead.
For other options for these commands, and for more information about their operations, see the man pages.
/ etc/inittab
So far, you may be wondering why you need to press the Ctrl-Alt-Delete key to restart on some systems, or how this runlevel setting is configured. Remember the id field in / etc/inittab? Of course, there are other fields in / etc/inittab, and some of these directories (such as rc1.d or rc5.d) have a series of init scripts, where the number is the level at which the script is applied. Listing 8 shows a complete inittab on our Fedora 8 system.
Listing 8. Complete inittab on Fedora 8
# # inittab This file describes how the INIT process should set up# the system in a certain run-level.## Author: Miquel van Smoorenburg,# Modified for RHS Linux by Marc Ewing and Donnie Barnes## Default runlevel. The runlevels used by RHS are:# 0-halt (Do NOT set initdefault to this) # 1-Single user mode# 2-Multiuser, without NFS (The same as 3 If you do not have networking) # 3-Full multiuser mode# 4-unused# 5-X1room6-reboot (Do NOT set initdefault to this) # id:5:initdefault:# System initialization.si::sysinit:/etc/rc.d/rc.sysinitl0:0:wait:/etc/rc.d/rc 0l1:1:wait:/etc/rc.d/rc 1l2:2:wait:/etc/rc.d/rc 2l3:3:wait:/etc/rc.d/rc 3l4:4:wait: / etc/rc.d/rc 4l5:5:wait:/etc/rc.d/rc 5l6:6:wait:/etc/rc.d/rc Trap CTRL-ALT-DELETEca::ctrlaltdel:/sbin/shutdown-T3-r now# When our UPS tells us power has failed Assume we have a few minutes# of power left. Schedule a shutdown for 2 minutes from now.# This does, of course, assume you have powerd installed and your# UPS connected and working correctly.pf::powerfail:/sbin/shutdown-f-h + 2 "Power Failure; System Shutting Down" # If power was restored before the shutdown kicked in, cancel it.pr:12345:powerokwait:/sbin/shutdown-c "Power Restored Shutdown Cancelled "# Run gettys in standard runlevels1:2345:respawn:/sbin/mingetty tty12:2345:respawn:/sbin/mingetty tty23:2345:respawn:/sbin/mingetty tty34:2345:respawn:/sbin/mingetty tty45:2345:respawn:/sbin/mingetty tty56:2345:respawn:/sbin/mingetty tty6# Run xdm in runlevel 5x:5:respawn:/etc/X11/prefdm-nodaemon
As usual, comments begin with #, and the other lines have several fields in the following format:
Id:runlevels:action:process
Id
Is a unique identifier consisting of 1 to 4 characters. The old version was limited to 2 characters, because you usually see that only 2 characters are used.
Runlevels
Lists the run levels at which activities against this id should operate. If no runlevels are listed, do so under all runlevels.
Action
Describe several possible actions
Process
When the operation is performed on this line, tell which process should run, if any.
Some common actions that may be specified in / etc/inittab are shown in Table 3. For other possibilities of inittab, see the man page.
Table 3. Some common inittab operations
Purpose
Respawn
Restart the process when it terminates. Typically used for the getty process, which is used to monitor logins.
Wait
Start the process as soon as a specific runlevel is entered, waiting for it to terminate until init starts running.
Once
Start the process as soon as a specific runlevel is entered.
Initdefault
Specify the run level to enter after the system boots.
Ctrlaltdel
When the init receives the SIGINT signal, it executes the relevant process, for example, someone presses CTRL-ALT-DEL on the system console.
Listing 9 shows only the entries in listing 8 for Ctrl-Alt-Delete. Now you can see why pressing the Ctrl-Alt-Delete key causes the system to restart.
Listing 9. Capture Ctrl-Alt-Delete
# Trap CTRL-ALT-DELETEca::ctrlaltdel:/sbin/shutdown-T3-r now
Initialization script
You may notice these lines in listing 8, such as
L5:5:wait:/etc/rc.d/rc 5
In this case, the / etc/rc.d/rc script (or command) will be run with parameter 5 regardless of whether the run level entered is 5 or not. Init will wait until the command completes before performing anything else.
The scripts used by init when starting the system, changing the runlevel, or shutting down are usually stored in the / etc/init.d or / etc/rc.d directory. A series of symbolic links under the rcn.d directory, with one directory for each runlevel n that controls whether the script starts when a runlevel is entered or stops when it leaves. These links start with a K or an S, followed by a two-digit number, followed by the service name, as shown in listing 10.
Listing 10. Init script
[root@pinguino ~] # find / etc-path "* rc [0-9] * .d /?? au*" / etc/rc.d/rc2.d/S27auditd/etc/rc.d/rc2.d/K72autofs/etc/rc.d/rc4.d/S27auditd/etc/rc.d/rc4.d/S28autofs/etc/rc.d/rc5.d/S27auditd/etc/rc.d/rc5.d/S28autofs/etc/rc.d/rc0.d/K72autofs/etc/rc.d / rc0.d/K73auditd/etc/rc.d/rc6.d/K72autofs/etc/rc.d/rc6.d/K73auditd/etc/rc.d/rc1.d/K72autofs/etc/rc.d/rc1.d/K73auditd/etc/rc.d/rc3.d/S27auditd/etc/rc.d/rc3.d/S28autofs [root@pinguino ~] # cd / etc/rc.d/rc5.d [root@pinguino rc5.d] # ls-l?? a*lrwxrwxrwx 1 root root 16 2008- 04-07 11:29 S27auditd->.. / init.d/auditdlrwxrwxrwx 1 root root 16 2008-04-01 07:51 S28autofs->.. / init.d/autofslrwxrwxrwx 1 root root 15 2008-04-01 14:03 S44acpid->.. / init.d/acpidlrwxrwxrwx 1 root root 13 2008-04-01 07:50 S95atd->.. / init.d/atdlrwxrwxrwx 1 root root 22 2008-04-01 07:54 S96avahi-daemon->.. / init.d/avahi-daemonlrwxrwxrwx 1 root root 17 2008-11 -17 13:40 S99anacron->.. / init.d/anacron
You can see that there are Knn entries for audit and autofs services at all runlevels and Snn entries under runlevels 3 and 5. S indicates that the service is started when the run level is entered, while K indicates that the service should be stopped. The nn part of the link name indicates the priority of starting or pausing the service. In this example, audit starts before autofs and stops later.
Refer to the man pages for more information about init and inittab.
Init vs. Upstart
As we have seen, the traditional method of booting the Linux system is based on the UNIX System V init process. It involves loading an initial RAM disk (initrd) and then passing the control to a program called init, usually installed as part of the sysvinit package. The init program then runs a series of scripts in a predefined order to start the system. If some of the expected information is not available, the init process waits until it is available. This is suitable for systems where everything is known and connected when the system starts. Systems with hot-swappable devices, network file systems, and even network interfaces that cannot be used at startup face new challenges. Indeed, waiting for hardware that may not be available for a long time, or even for a relatively long time, is not ideal.
Another initialization process, upstart, was first introduced in Ubuntu 6.10 ("Edgy Eft") in 2006. At present, the init process has been replaced in Ubuntu and Fedora, as well as other systems, although there are still traces of init, the power of upstart may not be realized for some time.
Compared to the static collection of init scripts used in earlier systems, upstart systems are event-driven, and events can be triggered by hardware changes, startup or shutdown or tasks, or by any other process on the system. Events are used to trigger tasks or services, collectively referred to as jobs. Thus, for example, connecting to a USB drive may cause the udev service to send a block-device-added event, which may cause a scheduled task to check / etc/fstab and mount the drive (if necessary). As another example, an Apache web server may be started only if the network and the required file system are available.
The upstart initialization program replaces / sbin/init. Upstart jobs are defined under the / etc/init directory and its subdirectories. The upstart system will currently handle / etc/inittab and System V init scripts. On systems such as recent Fedora versions, / etc/inittab may contain only id entries for initdefault operations. Currently, the Ubuntu system does not have / etc/inittab by default, so if you want to specify a default run level, you can create one.
Upstart also uses the initctl command to support interaction with the upstart init daemon. At this point you can start or terminate a job, list a job, get the status of the job, issue events, restart the init process, and so on. Listing 11 shows how to use initctl to get a list of upstart jobs on a Fedora 13 system.
Listing 11. Use initctl to interact with the upstart init daemon
[ian@echidna ~] $initctl listrc stop/waitingtty (/ dev/tty3) start/running, process 1486tty (/ dev/tty2) start/running, process 1484tty (/ dev/tty6) start/running, process 1492tty (/ dev/tty5) start/running, process 1490tty (/ dev/tty4) start/running, process 1488plymouth-shutdown stop/waitingcontrol-alt-delete stop/waitingsystem-setup-keyboard start/running, process 1000readahead-collector stop/waitingvpnc-cleanup stop/waitingquit-plymouth stop/waitingrcS stop/waitingprefdm start/running That's all for process 1479init-system-dbus stop/waitingck-log-system-restart stop/waitingreadahead stop/waitingck-log-system-start stop/waitingstart-ttys stop/waitingreadahead-disable-services stop/waitingck-log-system-stop stop/waitingrcS-sulogin stop/waitingserial stop/waiting 's article on "sample analysis of runlevels and restarts in Linux". Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.