systemctl 命令详解

引言

CentOS 7.0 中一个最主要的改变,就是切换到了systemd。它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服务进行管理。systemd兼容SysV和Linux标准组的启动脚本。

Systemd是一个Linux操作系统下的系统和服务管理器。它被设计成向后兼容SysV启动脚本,并提供了大量的特性,如开机时平行启动系统服务,按需启动守护进程,支持系统状态快照,或者基于依赖的服务控制逻辑。 先前的使用SysV初始化或Upstart的红帽企业版Linux版本中,使用位于/etc/rc.d/init.d/目录中的bash初始化脚本进行管理。而在RHEL 7/CentOS 7中,这些启动脚本被服务单元取代了。

服务单元以.service文件扩展结束,提供了与初始化脚本同样的用途。要查看、启动、停止、重启、启用或者禁用系统服务,你要使用systemctl 来代替旧的service命令。注:为了向后兼容,旧的service命令在CentOS 7中仍然可用,它会重定向所有命令到新的 systemctl 工具。

查看服务
# 查看所有服务
[root@hdp-01 ~]# service --status-all
# 或分页查看
[root@hdp-01 ~]# service --status-all | more
[root@hdp-01 ~]# service --status-all | less

# 查看正在运行服务
[root@hdp-01 ~]# service --status-all | grep running
#或
[root@hdp-01 ~]# initctl list
#或
[root@hdp-01 ~]# systemctl

# 查看某个服务
[root@hdp-01 ~]# service --status-all | grep crond
crond (pid  1216) is running...
#或
[root@hdp-01 ~]# service crond status
crond (pid  1216) is running...

# 查看服务启动配置
[root@hdp-01 ~]# chkconfig --list
启动服务
[root@hdp-01 ~]# systemctl start httpd.service
停止服务
[root@hdp-01 ~]# systemctl stop httpd.service
重启服务
[root@hdp-01 ~]# systemctl restart httpd.service
查看服务状态
[root@hdp-01 ~]# systemctl status httpd.service
控制服务是否开机启动
[root@hdp-01 ~]# systemctl enable httpd.service     # 开启开机启动服务
[root@hdp-01 ~]# systemctl disable httpd.service    # 取消开机启动服务
[root@hdp-01 ~]# systemctl is-enabled httpd.service # 查看服务是否开机启动
重新加载服务配置文件
[root@hdp-01 ~]# systemctl reload httpd.service
显示启动失败的服务
[root@hdp-01 ~]# systemctl --failed 
posted @ 2021-11-01 19:23  追こするれい的人  阅读(1682)  评论(0编辑  收藏  举报