鸟哥Linux私房菜读书笔记3:Linux正确的关机方法

       Linux中,由于是多用户操作环境,并且每个感都是后台执行的,因此我们不知道还有多少人同时在这个主机上工作,如果贸贸然关机,可能对他人造成很大的影响,比如文件系统的损坏等。

       这时,正确的关机方法就体现出重要性了。

       首先,正确的关机顺序:

  •        查看系统的状态

  •        通知在线用户关机的时刻

  •        使用正确的关机命令

 

查看系统的状态判断是否可以关机

        who: 查看当前在线的用户

        netstat -a: 查看网络的联机状态

        ps -aux: 查看后台执行的程序

 

通知在线用户关机时刻

       这个功能的话可用shutdown命令实现,shutdown命令不仅可以用来关机,还可以用来向用户发送消息

 

正确的关机命令

       shutdown/reboot/halt/poweroff 及 sync。

       

       先说sync,可用man sync去查看其基础信息, sync其实是强制对数据进行写入操作,大家都知道,为了提高速度,计算机并不会实时在硬盘上进行读写操作,而是借助缓存,将经常要用到的数据放入缓存中,内存直接从缓存中读取,而sync就是强制将缓存中的数据写入磁盘,最好在关机或重启前多执行几次,以防止数据更新不正常等问题。

 

       下面说常用的几个关机命令:

        1. shutdown: 安全关机,所有登录中的用户会收到相应提示,并且登录功能会被锁掉。shutdown可以指定关机时间,可以是立即关机,也可以指定某个特定时刻关机。所有的进程首先收到SIGTERM的提示即将关机,以方便有足够时间去保存文件等。shutdown是通过修改run level(level 0: 关机;level 1:单用户维护;level 6:重启)去执行关机、重启命令的,所以我们也可以通过手动修改run level去执行关机、重启的操作,如:

init 0

        将run level设置成0, 就会关机啦,执行改操作须有root权限。下面说说shutdown的参数:

/sbin/shutdown [-t sec] [-arkhncfFHP] time [warning-message]

        为了省事,直接man了一把,参数如下,比较常用的用红色标注出来了:

        -a              Use /etc/shutdown.allow.

 

       -t sec        Tell init(8)to wait sec seconds between sending processes the warning and the kill signal, before changing to another run-level.[等待sec秒后修改run-level,也就是等待sec秒后关机或重启]

 

        -k             Don't really shutdown; only send the warning messages to every-body.[不真正改变run-level,只发送警告信息]

 

        -r             Reboot after shutdown.[停掉服务后重启]

 

        -h            Halt or poweroff after shutdown.[停掉服务后关机]

 

        -H            Halt action is to halt or drop into boot monitor on systems that support it.

 

        -P            Halt action is to turn off the power.

 

       -n            [DEPRECATED] Don't callinit(8)to do the shutdown but do it ourself.The use of this option is discouraged, and its results are not always what you'd expect.[不通过调用init命令关机,而直接用shutdown来关机]

 

        -f             Skip fsck on reboot. [重启时不进行磁盘检测]

 

        -F            Force fsck on reboot. [重启时强制磁盘检测]

 

        -c            Cancel an already running shutdown. With this option it is of course not possible to give the time argument, but you can enter a explanatory message on the command line that will be sent to all users.[取消掉正在执行的shutdown]

                                                                                                                              

                                                                                                             

        来两个例子,第一个是立即关机:

shutdown -h now
第二个是发送警告信息:
shutdown -k now 'It's a warning message'

 

       2. Halt操作halt[停止系统,但不关闭电源], reboot[重启], poweroff[停止系统,并关闭电源]。这三个操作都会先在/var/log/wtmp中记录登出,然后再通知内核要做的操作。 /var/log/wtmp是一个记录每个用户登录时间和持续时间等信息的二进制文件,可用last命令查看其内容。

       如果在正常使用环境下调用halt/reboot/poweroff,系统会自动调用shutdown命令来完成操作。下面来看看参数吧:

/sbin/halt [-n] [-w] [-d] [-f] [-i] [-p] [-h]
/sbin/reboot [-n] [-w] [-d] [-f] [-i]
/sbin/poweroff [-n] [-w] [-d] [-f] [-i] [-h]

       -n     Don't sync before reboot or halt. Note that the kernel and stor-age drivers may still sync.

 

       -w     Don't actually reboot or halt but only write the wtmp record (in the /var/log/wtmp file).

 

       -d     Don't write the wtmp record. The -n flag implies -d.

 

       -f     Force halt or reboot, don't call shutdown(8).[直接使用halt或者reboot执行命令,不调用shutdown]

 

       -i     Shut down all network interfaces just before halt or reboot.

 

       -h     Put  allharddrives  onthe system in standby mode just before halt or poweroff.

 

       -p     When halting the system, do a poweroff. This is the default when halt is called as poweroff.

 

       3. 区别: 

       最大的区别的话应该是shutdown在关机前会先逐次关闭目前开启的服务,之后再执行关机操作,而Halt操作的话不会管那些开启着的服务,而是直接关机或重启。

 

posted @ 2013-07-06 20:38  geeky_jane  Views(360)  Comments(0Edit  收藏  举报