和Windows相比,Linux提供了多种关机、重启的命令,包括reboot/halt/shutdown等,而如果在服务器上,BMC还会提供ipmitool chassis power reset/ipmitool  chassis power cycle/ipmitool chassis power off等命令,那么面对这么多可行的命令,该如何选择呢?

    

    由于Linux内核和OS版本众多,不同的OS和内核版本上的reboot/halt/shutdown等命令实现方式可能会有差异,这就需要基于具体的OS和内核进行分析。下面基于CentOS 7,分享一下认识reboot/halt/shutdown差异的过程。


1.确定是否都是链接到同一个命令

    通过下面的命令可以看到,在CentOS 7上所有的reboot命令都链接到systemctl:

[root@localhost etc]# which shutdown

/usr/sbin/shutdown

[root@localhost etc]# which poweroff

/usr/sbin/poweroff

[root@localhost etc]# which reboot

/usr/sbin/reboot

[root@localhost etc]# cd /usr/sbin/

[root@localhost sbin]# ls -alrt shutdown

lrwxrwxrwx. 1 root root 16 Apr  5 13:07 shutdown -> ../bin/systemctl

[root@localhost sbin]# ls -alrt poweroff

lrwxrwxrwx. 1 root root 16 Apr  5 13:07 poweroff -> ../bin/systemctl

[root@localhost sbin]# ls -alrt reboot

lrwxrwxrwx. 1 root root 16 Apr  5 13:07 reboot -> ../bin/systemctl

[root@localhost sbin]# which systemctl

/usr/bin/systemctl



2.认识怎样关机保证不造成数据丢失

    除了要求能按照期望实现开关机、重启之外,在服务器尤其是存储服务器的工作环境下,用户对关机和重启能否造成数据丢失也特别关心。为此,弄清楚哪种使用方式不会造成数据丢失很有意义。还是以CentOS为例,根据systemctl的说明,reboot、halt与poweroff一样,只要不带 –force --force后缀,就不会造成数据丢失。下面是关于这些命令的具体说明:


halt

Shut down and halt the system. This is mostly equivalent to start halt.target --irreversible, but also prints a wall message to all users. If combined with --force, shutdown of all running services is skipped, however all processes are killed and all file systems are unmounted or mounted read-only, immediately followed by the system halt. If --force is specified twice, the operation is immediately executed without terminating any processes or unmounting any file systems. This may result in data loss.


poweroff

Shut down and power-off the system. This is mostly equivalent to start poweroff.target --irreversible, but also prints a wall message to all users. If combined with --force, shutdown of all running services is skipped, however all processes are killed and all file systems are unmounted or mounted read-only, immediately followed by the powering off. If --force is specified twice, the operation is immediately executed without terminating any processes or unmounting any file systems. This may result in data loss.


reboot [arg]

Shut down and reboot the system. This is mostly equivalent to start reboot.target --irreversible, but also rints a wall message to all users. If combined with --force, shutdown of all running services is skipped, however all processes are killed and all file systems are unmounted or mounted read-only, immediately followed by the reboot. If --force is specified twice, the operation is immediately executed without terminating any processes or unmounting any file systems. This may result in data loss.


Shutdown

    如果后面什么参数都不加,效果和shutdown -P +1相同,即先调用wall向所有登录的用户发出即将关机的信息,然后组织新的用户登录,接着等待60s,最后执行poweroff。因此,

区别于halt/poweroff,shutdown更安全,在不带任何参数的情况下,shutdown本质是delayed shutdown,当前我们的CentOS上默认是延迟60s后关机,具体请参考下面的命令和日志文件:

[root@localhost ~]# cat /var/log/messages | tail -n 2

Apr 28 22:50:30 localhost systemd: Started Session 195 of user root.

Apr 28 22:50:30 localhost systemd-logind: New session 195 of user root.

[root@localhost ~]# date

Thu Apr 28 22:51:25 CST 2016

[root@localhost ~]# shutdown -k

Shutdown scheduled for Thu 2016-04-28 22:52:30 CST, use 'shutdown -c' to cancel.

[root@localhost ~]#

Broadcast message from root@localhost.localdomain (Thu 2016-04-28 22:51:30 CST):


The system is going down for power-off at Thu 2016-04-28 22:52:30 CST!



[root@localhost ~]# cat /var/log/messages | tail -n 4

Apr 28 22:51:30 localhost systemd: Starting Delayed Shutdown Service...

Apr 28 22:51:30 localhost systemd: Started Delayed Shutdown Service.

Apr 28 22:51:30 localhost systemd-shutdownd: Shutting down at Thu 2016-04-28 22:52:30 CST (poweroff)...

Apr 28 22:51:30 localhost systemd-shutdownd: Creating /run/nologin, blocking further logins...


关于shutdown和poweroff/halt/reboot的关系可参考下面的说明:

SYNOPSIS

shutdown [OPTIONS...] [TIME] [WALL...]


DESCRIPTION

shutdown may be used to halt, power-off or reboot the machine.


The first argument may be a time string (which is usually "now"). Optionally, this may be followed by a wall

message to be sent to all logged-in users before going down.


The time string may either be in the format "hh:mm" for hour/minutes specifying the time to execute the

shutdown at, specified in 24h clock format. Alternatively it may be in the syntax "+m" referring to the

specified number of minutes m from now.  "now" is an alias for "+0", i.e. for triggering an immediate

shutdown. If no time argument is specified, "+1" is implied.


Note that to specify a wall message you must specify a time argument, too.


If the time argument is used, 5 minutes before the system goes down the /run/nologin file is created to ensure

that further logins shall not be allowed.


OPTIONS

The following options are understood:


--help

Print a short help text and exit.


-H, --halt

Halt the machine.


-P, --poweroff

Power-off the machine (the default).


-r, --reboot

Reboot the machine.


-h

Equivalent to --poweroff, unless --halt is specified.


-k

Do not halt, power-off, reboot, just write wall message.


--no-wall

Do not send wall message before halt, power-off, reboot.


-c

Cancel a pending shutdown. This may be used cancel the effect of an invocation of shutdown with a time

argument that is not "+0" or "now".


3.利用IPMI命令开关机

    如果机器支持BMC,可以通过BMC使用ipmitool去开关机。这种情况下,BMC直接关掉给host系统的DC电源,如果是chassis power reset/chassis power cycle还会间隔一会重新上电,因此在host系统上运行的应用程序和IO数据都来不及killed掉并同步到硬盘,这就会造成数据丢失。当是它的好处是:快捷,并且可以基于BMC Lan 接口去远程开关、重启机器。

这些命令包括:

ipmitool chassis power off #通过BMC关必DC

ipmitool chassis power on # 通过BMC给DC上电

ipmitool chassis power cycle # 先把DC掉店,间隔约1s后给DC上电

ipmitool chassis power reset # 先把DC掉店,然后给DC上电