init x, halt, shutdown, poweroff, reboot等之间的区别和联系

简介: init x, halt, shutdown, poweroff, reboot等之间的区别和联系

前言

最近这些天,每天晚上关机前,都会在osc上发一条动弹,“我要init 0了,各位晚安啊”,这是一件再正常不过的事情了。

image.png

看似很平常的一件事情,不过在昨晚就被一位同学的回复给难住了,到底是什么样的回复呢?让我们继续往下看吧。

image.png

从上图我们可以看到,那个回复就是“init 0和halt有区别不。。。。”,我一下就被这个问题给难住了,之前我了解的是init 0 最后调用了halt,但具体的什么区别和联系,真的还不太清楚哦。显然上述不足以回答该同学,且没有任何的说服力。所以带着这样的一个问题,开始了我的探索之路。


相关调研

遇到这个问题的时候,首先想到的还是借助于搜索引擎。最近一段时间我所使用的搜索引擎的顺序有点变化,以前是google-baidu,现在变成了bing-google-baidu(or yandex),不过这两天鉴于google.com.hk情况有点好转,最后还是首选了google一下上述的问题。


经过一番的调研和过滤,现将几个个人认为比较好的回答整理如下(如果想更进一步的了解,可以参考我之后的引用部分):


halt(1M) brings down the machine to the PROM (on SPARC) immediately, with very little regard to running the scripts in /etc/rc[0-6].d to take things down cleanly. reboot(1M) is the same, except instead of just stopping the OS, it reboots the machine.

halt命令直接将机器进入PROM,基本不执行/etc/rc[0-6]脚本去结束一些进程。reboot命令和它一样,除了结束运行操作系统并重启机器。


这种情况下某些进程由于非正常结束可能会导致数据的丢失。


注:PROM stands for programmable read-only memory


init(1M) would run the scripts in /etc/rc[0-6].d to (hopefully) take down the running processes cleanly, and then call halt, reboot, or poweroff(1M) depending on the argument you give it.

init命令将运行/etc/rc[0-6]脚本,该脚本将正常的结束所有正在运行的进程,然后根据你的命令行参数调用halt,reboot或者poweroff命令。


shutdown(1M) allows you to first give a time when the machine will shutdown, prints warnings to users logged in (and possibly to any NFS clients), and then calls init(1M). init then does things like described in the above paragraph.

shutdown命令在机器将要关机之前给了你一段时间,给那些已经登陆到系统的用户(和可能的NFS客户端)打印警告信息,然后调用init,接着init去做上述描述的事情。


Using shutdown or init is usually the best way to go about things AFAIK.

就我目前所知使用shutdown或者init是最好的方式。


注: AFAIK stands for As Far As I Konw


I try to keep in the habit of using shutdown(1M) since by default it gives a grace period before doing things. This way if you're /not/ on the machine you think you are (you're on production, and not a testing server), you still have an opportunity to cancel things before accidentally disrupting service.

综上所述, 基本的依赖关系为:

shutdown->init->/etc/rc[0-6]->halt/reboot/poweroff


扩张阅读

1M的含义

大家看到上述的halt(1M), init(1M), shutdown(1M)等一定都很好奇,这个1M是表示啥意思啊?下面我们将解答这个问题:


在linux下当我们遇到某命令不知道该如何使用的时候,linux为我们提供了一个非常方便的工具man,如man ls就可以查看ls命令的具体用法。但是有些命令比较复杂,会分成多个section来讲解。有时我们只关注某个section的内容,这时我们就需要用到man [1-8] ls这个命令了,如man 3 ls直接跳到section 3阅读。


从wikipedia上我们可以看到,linux类的一些系统有8个section。


The manual is generally split into eight numbered sections, organized as follows (on Research Unix, BSD, OS X and Linux):

Section Description
1 General commands
2 System calls
3 Library functions, covering in particular the C standard library
4 Special files (usually devices, those found in /dev) and drivers
5 File formats and conventions
6 Games and screensavers
7 Miscellanea
8 System administration commands and daemons

[from wikipedia]

而Unix System V使用的section与上面的有所区别,其中有一个section的名字就是1M,表示这个section描述的都是系统管理命令和一些daemon进程信息。

Unix System V uses a similar numbering scheme, except in a different order

Section Description
1 General commands
1M System administration commands and daemons
2 System calls
3 C library functions
4 File formats and conventions
5 Miscellanea
6 Games and screensavers
7 Special files (usually devices, those found in /dev) and drivers

[from wikipedia]

init level

由于我的系统是fedora,所以下面主要介绍redhat的init level信息:

Red Hat Linux/Fedora runlevels

Code Information
0 Halt
1 Single-user mode
2 Multi-user mode console logins only (without networking)
3 Multi-user mode, console logins only
4 Not used/User-definable
5 Multi-user mode, with display manager as well as console logins (X11)
6 Reboot

[from wikipedia]

结论

本文主要由一条动弹的回复,引出了init, halt ,reboot等命令之间的区别,并经过充分的调研,整理成博文。从上述的分析我们得出的结论是:最好使用shutdown命令来关机,因为他会在关机之前通知所有的已经登陆的用户和nfs client,正常的结束所有进程信息,正常关机。


不过对于我,单个用户且没有任何NFS client来说,使用init 0关机也没什么大碍,所有进程都会正常结束掉。


文中如有不同意见之处,欢迎留言。


引用

[1] http://unix.derkeiler.com/Newsgroups/comp.unix.solaris/2006-03/msg00380.html


[2] http://en.wikipedia.org/wiki/Man_page


[3] http://en.wikipedia.org/wiki/Runlevel


目录
相关文章
|
Docker Python 容器
Python项目打包成docker镜像,并在docker中运行
Python项目打包成docker镜像,并在docker中运行
2400 0
|
前端开发 安全 应用服务中间件
Nginx实现多域名转发到不同项目的配置
Nginx实现多域名转发到不同项目的配置
Nginx实现多域名转发到不同项目的配置
|
存储 Linux Shell
ESXi安装openwrt
在 ESXi 虚拟化环境下安装 OpenWrt 是一个常见的需求,特别是用于搭建虚拟路由器、网络测试等。下面给你详细步骤指导,帮你在 VMware ESXi 上成功安装并运行 OpenWrt 虚拟机。
|
6月前
|
人工智能 Linux API
DeepCodex零门槛配置:Codex无缝对接DeepSeek大模型完整指南
在AI编程开发场景中,Codex作为经典的代码生成助手,原生模型选择有限,而DeepSeek系列大模型凭借强大的代码理解、生成与推理能力,成为开发者的优质选择。DeepCodex作为轻量桥接工具,能解决两者协议不兼容问题,实现Codex与DeepSeek的无缝对接,让AI编程助手自由切换模型,满足不同开发场景需求。本文从准备工作、DeepCodex部署、模型配置、功能验证到常见问题排查,提供零门槛全流程操作指南,覆盖Windows、macOS、Linux多平台,新手也能快速完成配置。
1068 0
|
IDE Java 关系型数据库
如何利用Spring Initializr快速搭建项目
通过Spring Initializr可快速搭建Spring Boot项目。访问官网,配置项目信息,选择依赖(如Web、MyBatis、MySQL等),生成并下载项目压缩包,解压后导入IDE,自动构建完成,快速高效地初始化标准项目结构,提升开发效率。(238字)
1246 3
|
机器学习/深度学习 编解码 算法
【机器人路径规划】基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(Python代码实现)
【机器人路径规划】基于迪杰斯特拉算法(Dijkstra)的机器人路径规划(Python代码实现)
915 4
|
Ubuntu Linux
如何关闭或重新启动Ubuntu Linux?这四个命令希望你能熟练使用!
如何关闭或重新启动Ubuntu Linux?这四个命令希望你能熟练使用!
3890 0
如何关闭或重新启动Ubuntu Linux?这四个命令希望你能熟练使用!
|
网络协议 安全 Linux
Linux 防火墙解锁规则与 `firewall-cmd` 教程
【8月更文挑战第20天】
3341 0
|
存储 缓存 安全
Go 语言中的 Sync.Map 详解:并发安全的 Map 实现
`sync.Map` 是 Go 语言中用于并发安全操作的 Map 实现,适用于读多写少的场景。它通过两个底层 Map(`read` 和 `dirty`)实现读写分离,提供高效的读性能。主要方法包括 `Store`、`Load`、`Delete` 等。在大量写入时性能可能下降,需谨慎选择使用场景。
|
存储 Prometheus 监控
Prometheus 的报警机制:Alertmanager 的配置与使用
【8月更文第29天】Prometheus 是一个非常强大的监控系统,它不仅能够收集和存储时间序列数据,还能通过 Alertmanager 提供灵活的报警机制。Alertmanager 负责接收 Prometheus 发送的警报,并根据配置的规则执行相应的通知动作。本文将详细介绍如何配置 Alertmanager 以及如何使用它来实现基于 Prometheus 指标的报警通知。
5811 1

热门文章

最新文章