美文网首页
linux pid1 process(from sysvinit

linux pid1 process(from sysvinit

作者: 9_SooHyun | 来源:发表于2022-10-23 17:36 被阅读0次

linux操作系统初始化步骤:

  • 操作系统接管硬件以后,首先读入 /boot 目录下的内核文件
  • 仅仅将内核运行起来是毫无实际用途的,必须由 init 系统将系统代入可操作状态。比如启动外壳 shell 后,便有了人机交互,这样就可以让计算机执行一些预订程序完成有实际意义的任务。或者启动 X 图形系统以便提供更佳的人机界面,更加高效的完成任务。这里,字符界面的 shell 或者 X 系统都是一种预设的运行模式
    在内核加载完毕,并完成硬件检测与驱动程序加载后,此时主机硬件已经准备完毕,内核会主动呼叫第一个进程,也就是 /sbin/init,此配置文件最主要的功能就是准备软件执行的环境,包括系统的主机名、网络设定、语言、文件系统格式及其他服务的启动等。
    sysvinit 是 system V 风格的 init 系统,顾名思义,它源于 System V 系列 UNIX。它是已经风行了几十年的 UNIX init 系统,一直被各类 Linux 发行版所采用
    (后来,systemd has widely become the new standard for Linux distributions,/sbin/init 实际上软连接到/lib/systemd/systemd)。init进程的编号(pid)是1,其他所有进程都从它衍生,都是它的子进程
[root@VM-165-116-centos etc]# ls /sbin/init -l
lrwxrwxrwx 1 root root 22 Aug 18  2020 /sbin/init -> ../lib/systemd/systemd
[root@VM-165-116-centos etc]# 
[root@VM-165-116-centos etc]# ls /lib/systemd/systemd -l
-rwxr-xr-x 1 root root 1559656 Jun 18  2020 /lib/systemd/systemd
  • 确定运行级别。许多程序需要开机时启动,在Windows它们被叫做"服务"(service),在Linux就叫做"守护进程"(daemon)。sysvinit允许为不同的场合,分配不同的开机启动程序,这就叫做"运行级别"(runlevel)。也就是说,启动时根据"运行级别",确定要运行哪些程序
    每个运行级别在/etc目录下面,都有一个对应的子目录,指定要加载的程序,如/etc/rc2.d
    "rc",表示run command(运行程序),最后的d表示directory(目录)
[root@VM-165-116-centos etc]# ls -l /etc/rc2.d/
total 0
lrwxrwxrwx 1 root root 17 Aug 18  2020 K90network -> ../init.d/network
lrwxrwxrwx 1 root root 19 Aug 18  2020 S08bootlocal -> ../init.d/bootlocal
lrwxrwxrwx 1 root root 15 Oct  9 17:57 S12nslcd -> ../init.d/nslcd
lrwxrwxrwx 1 root root 21 Aug 18  2020 S50irqaffinity -> ../init.d/irqaffinity
[root@VM-165-116-centos etc]# 

/etc/rc2.d/存放的是启动脚本,S表示start,K表示kill,紧跟着的两位数表示优先级(越小越优先执行)
而实际上这些启动脚本都统一放在/etc/init.d/目录下,/etc/rcN.d都是通过软链引用这些脚本。这样一来,/etc/init.d/下修改一次脚本可以在全部/etc/rcN.d生效
(在systemd下,sysvinit的runLevel概念被target取代)

  • 用户登录进入login shell(在用户登录时打开的shell叫做login shell)

(1)命令行登录:init进程调用getty程序(意为get teletype),让用户输入用户名和密码。输入完成后,再调用login程序,核对密码(Debian还会再多运行一个身份核对程序/etc/pam.d/login)。如果密码正确,就从文件 /etc/passwd 读取该用户指定的shell,然后启动这个shell
(2)ssh登录:这时系统调用sshd程序(Debian还会再运行/etc/pam.d/ssh ),取代getty和login,然后启动shell
(3)图形界面登录:init进程调用显示管理器,Gnome图形界面对应的显示管理器为gdm(GNOME Display Manager),然后用户输入用户名和密码。如果密码正确,就读取/etc/gdm3/Xsession,启动用户的会话

参考:https://www.ruanyifeng.com/blog/2013/08/linux_boot_process.html


后来,systemd 取代了initd,成为系统的第一个进程(PID 为 1),其他进程都是它的子进程
systemd is an init system and system manager that has widely become the new standard for Linux distributions
注意,Systemd 提供了和 Sysvinit 兼容的特性。系统中已经存在的服务和进程无需修改。这降低了系统向 systemd 迁移的成本,使得 systemd 替换现有初始化系统成为可能


systemctlsystemd 的主命令,用于管理系统。systemctl --version查看Systemd的版本

常用的systemctl命令

# 重启系统
$ sudo systemctl reboot

# 关闭系统,切断电源
$ sudo systemctl poweroff

# CPU停止工作
$ sudo systemctl halt

# 暂停系统
$ sudo systemctl suspend

# 让系统进入冬眠状态
$ sudo systemctl hibernate

# 让系统进入交互式休眠状态
$ sudo systemctl hybrid-sleep

# 启动进入救援状态(单用户状态)
$ sudo systemctl rescue

Systemd 默认从目录/etc/systemd/system/读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/,真正的配置文件存放在那个目录
对于那些支持 Systemd 的软件,安装的时候,会自动在/usr/lib/systemd/system目录添加一个配置文件
配置文件的内容需要遵循Systemd的约定——

systemctl cat命令可以查看配置文件的内容

$ systemctl cat atd.service

[Unit]
Description=ATD daemon

[Service]
Type=forking
ExecStart=/usr/bin/atd

[Install]
WantedBy=multi-user.target

从上面的输出可以看到,配置文件分成几个区块。每个区块的第一行,是用方括号表示的区别名,比如[Unit]。注意,配置文件的区块名和字段名,都是大小写敏感的
每个区块内部是一些等号连接的键值对,等号两侧无空格

# 查看一个服务
$ sudo systemctl status httpd

# 立即启动一个服务
$ sudo systemctl start apache.service

# 立即停止一个服务
$ sudo systemctl stop apache.service

# 重启一个服务
$ sudo systemctl restart apache.service

# 杀死一个服务的所有子进程
$ sudo systemctl kill apache.service

# 重新加载一个服务的配置文件
$ sudo systemctl reload apache.service

# 重载所有修改过的配置文件
$ sudo systemctl daemon-reload

# 显示某个 Unit 的所有底层参数
$ systemctl show httpd.service

# 显示某个 Unit 的指定属性的值
$ systemctl show -p CPUShares httpd.service

# 设置某个 Unit 的指定属性
$ sudo systemctl set-property httpd.service CPUShares=500

如果你想让该软件开机启动,就执行下面的命令(以httpd.service为例)

$ sudo systemctl enable httpd

This will create a symbolic link from the system’s copy of the service file (usually in /usr/lib/systemd/system or /etc/systemd/system) into the location on disk, where systemd looks for autostart files (usually /etc/systemd/system/some_target.target.wants
上面的命令相当于在/etc/systemd/system/some_target.target.wants目录下添加一个符号链接,指向/usr/lib/systemd/system里面的httpd.service文件
这是因为开机时,Systemd只执行/etc/systemd/system目录里面的配置文件(这是一个典型的内聚和解耦的机制,聚焦于自身逻辑而不关注用户具体的配置)。这也意味着,如果把修改后的配置文件放在该目录,就可以达到覆盖原始配置的效果

参考:
https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html
https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units


service of sysvinit

sysvinit下的service管理命令:
service命令的作用是去/etc/init.d目录下寻找相应的服务,进行开启和关闭等操作

以iptables为例: service iptables start

  • 首先,sevice 会去/etc/init.d下寻找iptables脚本, start是iptables脚本里的一个参数
  • 然后告诉系统运行iptables这个脚本,剩下的事情就交给iptables脚本

所以,添加一个service也很简单——
编写一个脚本,然后把它放在/etc/init.d这个目录下,再用service + 脚本名字 运行即可
注意:在init.d里面得脚本是没有后缀名的

sysvinit 的优点是概念简单。Service 开发人员只需要编写启动和停止脚本,概念非常清楚;
将 service 添加/删除到某个 runlevel 时,也只需要执行一些创建/删除软连接文件的基本操作,不需要学习额外的知识或特殊的定义语法

systemd vs sysvinit, how to restart services

A systemd OS will use something like this for restarts:

systemctl restart httpd.service

The configfile is located at:

/etc/systemd/system/httpd.service

The traditional init.d(sysvinit) uses a more direct script restart with:

/etc/init.d/httpd restart

this is the same as:

service httpd restart

sysvinit 和 systemd 起停服务本质上都是去找文件

  • sysvinit直接找的就是服务的脚本文件
  • 而systemd找的是服务的配置文件,从配置文件里面捞启停命令

相关文章

网友评论

      本文标题:linux pid1 process(from sysvinit

      本文链接:https://www.haomeiwen.com/subject/ycpgzrtx.html