1. 背景
首先,我们先看一下/etc/init.d/README
内容:
You are looking for the traditional init scripts in /etc/rc.d/init.d,
and they are gone?
Here's an explanation on what's going on:
You are running a systemd-based OS where traditional init scripts have
been replaced by native systemd services files. Service files provide
very similar functionality to init scripts. To make use of service
files simply invoke "systemctl", which will output a list of all
currently running services (and other units). Use "systemctl
list-unit-files" to get a listing of all known unit files, including
stopped, disabled and masked ones. Use "systemctl start
foobar.service" and "systemctl stop foobar.service" to start or stop a
service, respectively. For further details, please refer to
systemctl(1).
Note that traditional init scripts continue to function on a systemd
system. An init script /etc/rc.d/init.d/foobar is implicitly mapped
into a service unit foobar.service during system initialization.
根据上面的解释,目前RED HAT 7, Centos 7等都采用systemd方式来作为系统初始化工具.
2. 为何替换使用systemd
systemd
通过并行启动服务来加速系统启动时间;systemd
使用Cgroup
可以更好的管理程序,同时也能控制执行环境;systemd
通过结合journald
达到更好的日志机制,这种日志方式在Red Hat Enterprise Linux 7中引入;systemd
可以处理系统配置动态调整;
3. systemd深入
systemd
管理服务,这些服务的定义通过unit files
unit file
Unit file存放在如下几个目录里面,systemd
以如下的顺序来读取unit files
/etc/systemd/system
/run/systemd/system
/usr/lib/systemd/system
同样,systemd
可以针对用户上下文下设置unit files,路径如下:
/etc/systemd/user
/run/systemd/user
/usr/lib/systemd/user
查看系统所有的unit files
systemd list-unit-files
unit files 分为service
和target
两类
systemctl list-unit-files --type service
systemctl list-unit-files --type target
1. 启动或暂时停用
systemctl start serviceX
systemctl stop serviceX
2. 设置服务自启动或者剔除自启动
systemctl enable serviceX
systemctl disable serviceX
3. 查看服务的依赖
systemctl list-dependencies serviceX
4. 终极停用或者撤销终极停用
systemctl mask serviceX
systemctl unmask serviceX