设为首页 收藏本站
查看: 2053|回复: 0

[经验分享] Reboot/halt/shutdown command on CentOS 7

[复制链接]

尚未签到

发表于 2018-4-26 11:07:00 | 显示全部楼层 |阅读模式
  

  和Windows相比,Linux提供了多种关机、重启的命令,包括reboot/halt/shutdown等,而如果在服务器上,BMC还会提供ipmitoolchassis power reset/ipmitool  chassis power cycle/ipmitool chassispower off等命令,那么面对这么多可行的命令,该如何选择呢?
  由于Linux内核和OS版本众多,不同的OS和内核版本上的reboot/halt/shutdown等命令实现方式可能会有差异,这就需要基于具体的OS和内核进行分析。下面基于CentOS7,分享一下认识reboot/halt/shutdown差异的过程。

  

  1.确定是否都是链接到同一个命令
  通过下面的命令可以看到,在CentOS7上所有的reboot命令都链接到systemctl:
[root@localhostetc]# which shutdown

  /usr/sbin/shutdown
[root@localhostetc]# which poweroff

  /usr/sbin/poweroff
[root@localhostetc]# which reboot

  /usr/sbin/reboot
[root@localhostetc]# cd /usr/sbin/

[root@localhostsbin]# ls -alrt shutdown

  lrwxrwxrwx.1 root root 16 Apr  5 13:07 shutdown -> ../bin/systemctl
[root@localhostsbin]# ls -alrt poweroff

  lrwxrwxrwx.1 root root 16 Apr  5 13:07 poweroff -> ../bin/systemctl
[root@localhostsbin]# ls -alrt reboot

  lrwxrwxrwx.1 root root 16 Apr  5 13:07 reboot -> ../bin/systemctl
[root@localhostsbin]# which systemctl

  /usr/bin/systemctl
  

  

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

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

  poweroff
  Shut downand power-off the system. This is mostly equivalent to startpoweroff.target --irreversible, but also prints a wall message to allusers. If combined with --force, shutdown of all running services isskipped, however all processes are killed and all file systems areunmounted or mounted read-only, immediately followed by the poweringoff. If --force is specified twice, the operation is immediatelyexecuted without terminating any processes or unmounting any filesystems. This may result in data loss.
  

  reboot[arg]
  Shut downand reboot the system. This is mostly equivalent to startreboot.target --irreversible, but also rints a wall message to allusers. If combined with --force, shutdown of all running services isskipped, however all processes are killed and all file systems areunmounted or mounted read-only, immediately followed by the reboot.If --force is specified twice, the operation is immediately executedwithout 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本质是delayedshutdown,当前我们的CentOS上默认是延迟60s后关机,具体请参考下面的命令和日志文件:
[root@localhost~]# cat /var/log/messages | tail -n 2

  Apr28 22:50:30 localhost systemd: Started Session 195 of user root.
  Apr28 22:50:30 localhost systemd-logind: New session 195 of user root.
[root@localhost~]# date

  ThuApr 28 22:51:25 CST 2016
[root@localhost~]# shutdown -k

  Shutdownscheduled for Thu 2016-04-28 22:52:30 CST, use 'shutdown -c' tocancel.
[root@localhost~]#

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

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

  

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

  Apr28 22:51:30 localhost systemd: Starting Delayed Shutdown Service...
  Apr28 22:51:30 localhost systemd: Started Delayed Shutdown Service.
  Apr28 22:51:30 localhost systemd-shutdownd: Shutting down at Thu2016-04-28 22:52:30 CST (poweroff)...
  Apr28 22:51:30 localhost systemd-shutdownd: Creating /run/nologin,blocking further logins...
  

  关于shutdown和poweroff/halt/reboot的关系可参考下面的说明:
  SYNOPSIS
  shutdown[OPTIONS...] [TIME] [WALL...]
  

  DESCRIPTION
  shutdown maybe used to halt, power-off or reboot the machine.
  

  The firstargument may be a time string (which is usually "now").Optionally, this may be followed by a wall
  message to besent to all logged-in users before going down.
  

  The timestring may either be in the format "hh:mm" for hour/minutesspecifying the time to execute the
  shutdown at,specified in 24h clock format. Alternatively it may be in the syntax"+m" referring to the
  specifiednumber of minutes m from now.  "now" is an alias for "+0",i.e. for triggering an immediate
  shutdown. Ifno time argument is specified, "+1" is implied.
  

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

  If the timeargument is used, 5 minutes before the system goes down the/run/nologin file is created to ensure
  that furtherlogins shall not be allowed.
  

  OPTIONS
  The followingoptions are understood:
  

  --help
  Print ashort help text and exit.
  

  -H, --halt
  Halt themachine.
  

  -P,--poweroff
  Power-offthe machine (the default).
  

  -r, --reboot
  Reboot themachine.
  

  -h
  Equivalentto --poweroff, unless --halt is specified.
  

  -k
  Do nothalt, power-off, reboot, just write wall message.
  

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

  -c
  Cancel apending shutdown. This may be used cancel the effect of an invocationof shutdown with a time
  argumentthat is not "+0" or "now".
  

  3.利用IPMI命令开关机
  如果机器支持BMC,可以通过BMC使用ipmitool去开关机。这种情况下,BMC直接关掉给host系统的DC电源,如果是chassispower reset/chassis power cycle还会间隔一会重新上电,因此在host系统上运行的应用程序和IO数据都来不及killed掉并同步到硬盘,这就会造成数据丢失。当是它的好处是:快捷,并且可以基于BMCLan 接口去远程开关、重启机器。
  这些命令包括:
  ipmitoolchassis power off #通过BMC关必DC
  ipmitoolchassis power on # 通过BMC给DC上电
  ipmitoolchassis power cycle # 先把DC掉店,间隔约1s后给DC上电
  ipmitoolchassis power reset # 先把DC掉店,然后给DC上电

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-452212-1-1.html 上篇帖子: CentOS 7安装、部署Glassfish 下篇帖子: CentOS 6,7系统服务管理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表