1、概述
CentOS 7使用Systemd管理守护进程。centos7采用 systemd管理,服务独立的运行在内存中,服务响应速度快,但占用更多内存。独立服务的服务启动脚本都在目录 /usr/lib/systemd/system里。Systend的新特性:
- 系统引导时实现服务的并行启动; 
- 按需激活进程; 
- 系统实现快照; 
- 基于依赖关系定义服务的控制逻辑; 
systemctl可用于内省和控制“systemd”系统和服务管理器的状态。centos7.x系统环境下我们经常使用此命令启停服务,实际上此命令除了其他独立服务还有很多其他用途。
2、systemctl参数说明
基本语法:systemctl start | stop | restart | status | reload 服务名
systemctl 指令管理的服务在 /usr/lib/systemd/system
查看服务的方法:pwd /usr/lib/systemd/system
2.1、使用语法
用法:systemctl [OPTIONS…] {COMMAND} …
2.2、参数说明
| 参数 | 参数说明 | 
| start | 立刻启动后面接的unit | 
| stop | 立刻关闭后面接的unit | 
| restart | 立刻关闭后启动后面接的unit,亦即执行stop再start的意思 | 
| reload | 不关闭后面接的unit的情况下,重载配置文件,让设定生效 | 
| enable | 设定下次开机时,后面接的unit会被启动 | 
| disable | 设定下次开机时,后面接的unit 不会被启动 | 
| status | 目前后面接的这个unit 的状态,会列出是否正在执行、是否开机启动等信息。 | 
| is-active | 目前有没有正在运行中 | 
| is-enable | 开机时有没有预设要启用这个unit | 
| kill | 不要被kill这个名字吓着了,它其实是向运行unit的进程发送信号 | 
| show | 列出unit的配置。 | 
| mask | 注销unit,注销后你就无法启动这个unit了 | 
| unmask | 取消对unit的注销 | 
| list-units | 依据unit列出目前有启动的unit。若加上–all才会列出没启动的。(等价于无参数) | 
| list-unit-files | 列出所有以安装unit以及他们的开机启动状态(enabled、disabled、static、mask)。 | 
| –type=TYPE | 就是unit type,主要有service,socket,target等 | 
| get-default | 取得目前的 target | 
| set-default | 设定后面接的 target 成为默认的操作模式 | 
| isolate | 切换到后面接的模式 | 
2.3、unit file结构
文件通常由三部分组成:
- Unit: 定义与Unit类型无关的通用选项;用于提供unit的描述信息,unit行为及依赖关系等。 
- Service:与特定类型相关的专用选项;此处为Service类型。 
- Install:定义由"systemctl enable"及"systemctl disable"命令在实现服务启用或禁用时用到的一些选项。 
2.4、Unit段的常用选项
- Description:描述信息,意义性描述; 
- After:定义unit的启动次序;表示当前unit应晚于哪些unit启动;其功能与Before相反; 
- Requies:依赖到其它的units;强依赖,被依赖的units无法激活时,当前的unit即无法激活; 
- Wants:依赖到其它的units;弱依赖; 
- Confilcts:定义units 的冲突关系; 
2.5、Service段的常用选项
- Type:用于定义影响ExecStart及相关参数的功能的unit进程类型; 类型有:simple、forking、oneshot、dbus、notify、idle。 
- EnvironmentFile:环境配置文件; 
- ExecStart:指明启动unit要运行的命令或脚本;ExecStart, ExecStartPost 
- ExecStop:指明停止unit要运行的命令或脚本; 
- Restart: 
2.6、Install段的常用配置
- Alias: 
- RequiredBy:被哪些unit所依赖; 
- WantBy:被哪些unit所依赖; 
2.7、Unit文件样例
[root@liuchao ~]# cd /usr/lib/systemd/system
[root@liuchao system]# cat chronyd.service
[Unit]
Description=NTP client/server
Documentation=man:chronyd(8) man:chrony.conf(5)
After=ntpdate.service sntp.service ntpd.service
Conflicts=ntpd.service systemd-timesyncd.service
ConditionCapability=CAP_SYS_TIME
[Service]
Type=forking
PIDFile=/var/run/chronyd.pid
EnvironmentFile=-/etc/sysconfig/chronyd
ExecStart=/usr/sbin/chronyd $OPTIONS
ExecStartPost=/usr/libexec/chrony-helper update-daemon
PrivateTmp=yes
ProtectHome=yes
ProtectSystem=full
[Install]
WantedBy=multi-user.target
[root@liuchao system]#3、systemctl使用示例
3.1、查看开机启动列表
 systemctl list-unit-files [ | grep 服务名] (查看服务开机启动状态, grep 可以进行过滤)
[root@localhost ~]# systemctl list-unit-files
[root@localhost ~]# systemctl list-unit-files | grep firewalld
firewalld.service                             disabled
#查看已启动的服务列表
systemctl list-unit-files|grep enabled
#
显示所有已启动的服务
systemctl list-units --type=service
 
   可以 写一半再查看完整的服务名,一般也可以简写:firewalld.service = firewall
 
   3.2、设置开机启动
systemctl在enable、disable、mask子命令里面增加了–now选项,可以激活同时启动服务,激活同时停止服务等。
# 设置开机启动并现在启动
## 相当于同时执行了systemctl start 服务名
systemctl enable --now firewalld
# 查看服务启动状态
root@localhost ~]# systemctl status firewalld3.3、取消开机启动
# 取消开机启动并现在就停止服务
systemctl disable --now firewalld
## 查看服务状态是否停止
[root@localhost ~]# systemctl status firewalld
# 查看启动列表
[root@localhost ~]# systemctl list-unit-files |grep firewalld
firewalld.service                             disabled3.4、开启服务
systemctl start firewall 
   3.5、关闭服务(但是下次开机还是会启动)
systemctl stop firewall关闭防火墙:
 
   3.6、重启服务
systemctl restart 服务名3.7、重新加载配置
systemctl reload 服务名3.8、输出服务运行的状态
systemctl status 服务名
systemctl status firewalld查看防火墙的状态,现在是运行中:
 
   3.9、检查service是否在启动状态
# systemctl is-active 服务名
systemctl is-active NetworkManager
# active3.10、检测unit单元是否为自动启动
# systemctl is-enabled 服务名
systemctl is-enabled firewalld
# enabled 
   3.11、注销一个服务(service)
systemctl mask 是注销服务的意思。 注销服务意味着: 该服务在系统重启的时候不会启动 该服务无法进行做systemctl start/stop操作 该服务无法进行systemctl enable/disable操作
systemctl mask firewalld3.12、取消注销服务(service)
systemctl unmask firewalld3.13、显示单元的手册页(前提是由unit提供)
systemctl help3.14、重新加载所有修改过的配置文件
当新增或修改service单元文件时,需要系统重新加载所有修改过的配置文件
systemctl daemon-reload3.15、查看systemd资源使用率
systemd-cgtop3.16、杀死服务
[root@s153 system]# systemctl kill xinetd
[root@s153 system]# systemctl is-failed xinetd
inactive







![windows本地开发Spark[不开虚拟机]](https://img-blog.csdnimg.cn/a75e70f97c4b41be9ca4e040f9bfa151.png)










