linux pid1 process(from sysvinit

2022-10-23  本文已影响0人  9_SooHyun

linux操作系统初始化步骤:

[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
[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取代)

(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

所以,添加一个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 起停服务本质上都是去找文件

上一篇 下一篇

猜你喜欢

热点阅读