systemd是几乎所有最新的Linux发行版采用的初始化系统,是用ps命令可以看到看到在Centos中的PID(进程号)为1,因此所有其他的进程都属于他的子进程。

systemd并不是一个命令,它包含了一组系统命令,同时,systemd是基于事件的,它可以使进程并行启动并且可以精确管理各种依赖关系。

systemd和System V的对比

System V是串行启动进程的,也就是说,只有上一个进程启动之后,才可以启动下一个进程,而systemd可以使进程同时启动,同时systemd支持重启因错误而停止的进程,管理任务的计划,系统日志,外设等。

System V使用Run Level(运行级别)来管理不同的进程组,而systemd用target来替换了Syatem V的运行级别,其中target是指“目标”,简单来说是多个unit(单元)构成的一个组。

systemd的target名称

System V运行级别

作用

poweroff.target

0

关机

rescue.target

1

单用户模式

multi-user.target

2

等同于级别3

multi-user.target

3

多用户的命令行界面

multi-user.target

4

等同于级别3

graphical.target

5

多用户的图形界面

reboot.target

6

重启

emergency.target

emergency

紧急shell

systemd管理进程的启动和停止

systemd提供了systemctl命令,使用这个命令使得用户可以管理unit(单元),对于systemd来说,unit泛指它可以操作的任何对象,包括服务、挂载、外设等等。

systemctl对进程操作的命令如下所示:

systemd命令

System V命令

作用

systemctl start toto

service toto start

启动服务

systemctl stop toto

service toto stop

停止服务

systemctl restart toto

service toto restart

重启服务

systemctl status toto

service toto status

查看服务状态

systemctl reload toto

service toto reload

重载配置文件

(不停止服务)

systemctl enable toto

chkconfig toto on

开机自动启动服务

systemctl disable toto

chkconfig toto off

开机不自动启动服务

systemctl is-enable toto

chkconfig toto

查看服务是否开机自启动

systemctl list-unit-files --type=service

chkconfig --list

查看各个级别下服务的启动和禁用情况

如果不带参数运行systemctl命令,系统会自动运行以下命令:

systemctl list-units

这个命令的意思是列出所有的活动单元,按空格键可以翻页,q键退出。使用如下命令可以过滤信息:

systemctl list-units --type=你要查找的类型

使用target

使用systemd可以存在多个活动的target