📄 lsg15.htm
字号:
<FONT COLOR="#000080">shutdown 15 'Backup Time!'</FONT></PRE>
<P>shuts down the system after 15 minutes and display the message "Backup Time!" to all users on the system, prompting them to log off. This command is handy when you enforce a policy of shutting down at specific intervals, either for maintenance or backups.
<BR>
<P>In most versions of Linux, the shutdown command accepts the -r option. This option causes the PC to reboot after the shutdown has occurred. You can use this option to reboot to another operating system or to restart Linux after making changes to the kernel or devices. You can use the -r option with a time or message, if you want. The command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">shutdown -r 5</FONT></PRE>
<P>reboots the system after five minutes.
<BR>
<P>In most cases, using Ctrl+Alt+Del or the shutdown command results in the display of a number of status messages on the main console. When Linux has finished shutting down the system, you see the message
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">The system is halted</FONT></PRE>
<P>When this message appears on-screen, it is safe to shut off the system power or reboot the machine. Although it may seem a little strange to have to follow these extra steps, you will find that many high-end operating systems such as UNIX (and even Windows NT and Windows 95) require you to follow a specific shutdown procedure to prevent loss of information. Get in the habit!
<BR>
<BR>
<A NAME="E68E84"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Understanding the init Daemon</B></FONT></CENTER></H3>
<BR>
<P>The init daemon is usually invoked as the last step in the booting of the Linux kernel. The init daemon is one of the most important Linux daemons because it creates processes for the rest of the system. The init daemon is executed when Linux starts and stays active until Linux is shut down. Understanding what init (and its linked utility telinit) does and how it controls the operating system is important to better administering the Linux system.
<BR>
<P>Both init and telinit use several configuration files to perform their tasks, so the following sections look at those files in detail, too. These files are closely involved in the starting and stopping of terminals and console sessions. The init program is usually kept in the /bin directory, although some versions of Linux keep it in /sbin. The same directories apply to the telinit utility. The configuration files are always kept in /etc, though.
<BR>
<BR>
<A NAME="E69E110"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Run Levels</B></FONT></CENTER></H4>
<BR>
<P>When the init daemon is executed, it reads instructions from the file /etc/inittab, which is primarily used to start getty processes for terminals and other processes required by Linux. While examining the /etc/inittab file, init searches for an entry labeled initdefault, which sets the default initial run level of the system. If no initdefault level is defined in the /etc/inittab file, the system prompts for the level.
<BR>
<P>A run level is a particular set of processes ranging from a bare minimum for system administration only to a full operation supporting all configured devices. Run levels are defined by a number from zero to six. An additional superuser level (often called single-user level as only root can log in) is defined as <I>s</I>. The init daemon knows which processes are associated with each run level from information in the /etc/inittab file.
<BR>
<P>When you use the s run level to display the system in single-user mode, the /etc/inittab file is not read. Instead, the utility /bin/su is invoked for the system console (defined by /dev/console). The init process can save the current state of a system when instructed to change to single-user mode from a higher run level. With some versions of init, the current state of the system is saved in a file called /etc/ioctl.save by the program ioctl. When the console is restarted to a higher run level, the states in this file are restored. If no ioctl.save file is found, the default states are used.
<BR>
<P>When starting up into multiuser mode (run levels higher than single-user mode), the init daemon performs any entries identified by the instructions boot and bootwait in the /etc/inittab file. Following these instructions usually allows filesystems to be mounted. After these instructions are processed, the rest of the entries that match the selected run level are executed.
<BR>
<P>The run level of the system can be changed by a user with access to the commands that affect the level. This access is usually restricted to the system administrator for security reasons. You change the run level by using the utility program /etc/telinit (which is linked to /etc/init). The telinit utility is responsible for sending messages to the init daemon to alter the current run level to the requested new level. To alter the run level, add the required level (zero through six or s) to the telinit command. For example, the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">telinit 2</FONT></PRE>
<P>changes the run level to level two and causes init to reread the /etc/inittab file and execute all processes for that level or terminate those for higher levels. To drop into superuser (single-user) mode, use the s option:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">telinit s</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>When switching to the superuser level, you can use either an uppercase or lowercase s. The telinit and init utilities can handle both cases.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>You can specify a time delay for the change, in seconds, after the -t option. The command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">telinit -t5 3</FONT></PRE>
<P>changes the run level to level three after five seconds. If no time is specified, Linux uses the default value of 20 seconds.
<BR>
<P>When you change the run level, init sends a SIGTERM warning signal to all processes that are not valid with the new run level. After sending the SIGTERM signal, init waits the specified number of seconds (or the default 20 seconds), and then terminates the process forcibly.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>If a process started by init has spawned new processes that are not in the same process group, init doesn't terminate them when you change the run level. You must terminate these processes manually.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E111"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The /etc/inittab File</B></FONT></CENTER></H4>
<BR>
<P>As mentioned previously, the /etc/inittab file is tied closely to the init daemon. Look at the /etc/inittab file on your system to understand which processes are started and which run level is invoked when Linux starts. Extracts from a sample /etc/inittab file show the processes involved in starting the system with init. The first section in the sample /etc/inittab file identifies the default run level, in this case level five:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">id:5:initdefault:</FONT></PRE>
<P>The next section in the /etc/inittab file handles the system startup through the files in the /etc/rc.d directories:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">si:S:sysinit:/etc/rc.d/rc.S</FONT></PRE>
<P>Following this section is a pointer to the file /etc/rc.d/rc.K, which is used when the system enters the single-user run level:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">su:S:wait:/etc/rc.d/rc.K</FONT></PRE>
<P>Next is a pointer to the file /etc/rc.d/rc.M for when the system is started in multiuser level (any one of the levels one through six):
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">rc:123456:wait:/etc/rc.d/rc.M</FONT></PRE>
<P>The most common run level is five, which is the normal operating level for Linux in multiuser mode. Most installations seldom use the other levels, although they can be used in some circumstances to control access to peripherals. Run levels are best left as the system wants them to prevent problems. This means using run level s for superuser mode and run level five for general use.
<BR>
<P>Because Linux runs on PC machines, it can support the "three-fingered salute" or Ctrl+Alt+Del sequence. This sequence is not usually supported on PC UNIX systems, so a special instruction is mapped to the sequence in the /etc/inittab file:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">ca::ctrlaltdel:/sbin/shutdown -t3 -rf now</FONT></PRE>
<P>When the Ctrl+Alt+Del sequence is intercepted, the system begins a shutdown as shown by the command at the end of the preceding line.
<BR>
<P>The /etc/inittab file then holds an instruction to start a getty process for each terminal and virtual screen on the system. This sample /etc/inittab file starts six virtual screens (tty1 through tty6) and two serial lines (ttyS0 and ttyS1):
<BR>
<PRE>
<FONT COLOR="#000080">c1:12345:respawn:/sbin/agetty 38400 tty1
c2:12345:respawn:/sbin/agetty 38400 tty2
c3:45:respawn:/sbin/agetty 38400 tty3
c4:45:respawn:/sbin/agetty 38400 tty4
c5:45:respawn:/sbin/agetty 38400 tty5
c6:456:respawn:/sbin/agetty 38400 tty6
s1:45:respawn:/sbin/agetty 19200 ttyS0
s2:45:respawn:/sbin/agetty 19200 ttyS1</FONT></PRE>
<P>The terminal and serial line instructions are examined in more detail in <A HREF="lsg11.htm">Chapter 11</A>, "Terminals and term."
<BR>
<P>The lines in the /etc/inittab file follow a specific format. The format follows this pattern:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">ID:runlevel:action:process</FONT></PRE>
<P>The ID is a one- or two-character string that uniquely identifies the entry. In most cases, this string corresponds to the device name, such as 1 for tty1, 2 for tty2, and so on. The runlevel decides which of the run levels the line applies to (varying from zero to six). If no entry is provided, then all run levels are supported. Multiple run levels may be identified in the field.
<BR>
<P>The action indicates the command to execute when init reads the line. The following items are all valid entries for the action field:
<BR>
<UL>
<LI>The boot action runs when inittab is first read.
<BR>
<BR>
<LI>The bootwait action runs when inittab is first read.
<BR>
<BR>
<LI>The initdefault action sets the initial run level.
<BR>
<BR>
<LI>The off action terminates the process if it is running.
<BR>
<BR>
<LI>The once action starts the process once.
<BR>
<BR>
<LI>The ondemand action always keeps the process running (the same as respawn).
<BR>
<BR>
<LI>The powerfail action executes when init gets a power fail signal.
<BR>
<BR>
<LI>The powerwait action executes when init gets a power fail signal.
<BR>
<BR>
<LI>The sysinit action executes before accessing the console.
<BR>
<BR>
<LI>The respawn action always keeps the process running.
<BR>
<BR>
<LI>The wait action starts the process once.
<BR>
<BR>
</UL>
<P>If init senses a powerfail condition (such as termination of power to the PC signaled by an Uninterruptible Power Supply) and the system was in multiuser mode, some special powerfail conditions are executed upon restart. These conditions usually check the filesystem for problems prior to bringing the system back up. The /etc/inittab file can contain specific instructions for these conditions, as shown in the following code:
<BR>
<PRE>
<FONT COLOR="#000080"># What to do when power fails (shutdown to single user).
pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"
# If power is back before shutdown, cancel the running shutdown.
pg:0123456:powerokwait:/sbin/shutdown -c "THE POWER IS BACK"
# If power comes back in single user mode, return to multi user mode.
ps:S:powerokwait:/sbin/init 5</FONT></PRE>
<P>All these powerfail conditions assume that some device manages to send the powerfail signals to the init process. Special device drivers that interface with UPSs usually do this.
<BR>
<P>The init daemon doesn't terminate when it has finished reading /etc/inittab. It stays active and monitors the system for specific instructions to change the run level (from a telinit command). It is also responsible for watching all the processes it started, including the getty processes for terminals. Whenever a process init started (called a child process, with init as the parent process) is terminated for any reason, init records the event and a reason for the termination (if possible to identify) in the files /etc/utmp and /etc/wtmp.
<BR>
<P>Whenever init senses the termination of a a child process, a power fail signal, or a run level change, it rereads the /etc/inittab to check for instructions. You can make changes to the inittab file (using any ASCII editor) while the system is running, but the changes will not be effective until the system reboots or one of the reread conditions occurs. An alternative is to use the q argument to force init to reexamine the /etc/inittab file. To force a reread of the /etc/inittab file, issue the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">init q</FONT></PRE>
<P>The init process checks how many times it has to restart (respawn) a process. If a process must be restarted more than 10 times in a two minute period, init assumes that there is an error in the command line of /etc/inittab for that process and generates an error message on the system console. The init process then refuses to respawn that process for five minutes or until it's forced to restart by the superuser. This step is useful because it prevents the system from wasting CPU cycles when a typographic error was made in the /etc/inittab file.
<BR>
<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -