centos7 中 systemd systemctl管理服务的命令

背景: centos7 和 6的重大改变

对于系统管理员而言,系统的服务管理是一件很日常和很重要的工作,而7在6的基础上有了很大的改变,就连命令都完全不一样了。所以要拥抱变化,学习7是如何进行服务的管理和控制的。

system和systemctl的初探

Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

在Linux生态系统中,Systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。Systemd通常是所有其它守护进程的父进程,但并非总是如此。

【第一步】

查看systemd的版本

# systemctl --version

systemd 219

+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

查看进程是否存在

ps -ef | grep [s]ystemd

注意:systemd是作为父进程(pid=1)运行的

分析systemd的启动进程

# systemd-analyze

Startup finished in 639ms (kernel) + 931ms (initrd) + 12.432s (userspace) = 14.003s

分析各个进程启动发费的时间

#systemd-analyze blame

分析启动时候的关键链

systemd-analyze critical-chain

重要:Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。

列出所有服务可用单元

systemctl list-unit-files

(200多个)

列出所有运行中的单元

systemctl list-units

列出失败的单元?

systemctl --failed

列出某个单元是否启动

# systemctl is-enabled crond.service

enabled

或者

# systemctl is-enabled crond

enabled

检查某个单元或服务是否运行

# systemctl is-active crond

active

或者

# systemctl status crond

这个信息更详细

############################# 控制服务  ############################

列出所有服务(包括启用的和禁用的)

systemctl list-unit-files  --type=service

(120+)

以httpd为例

yum install httpd

会生成以下文件

/usr/lib/systemd/system/httpd.service

Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

[[email protected] ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

Active: inactive (dead)

Docs: man:httpd(8)

man:apachectl(8)

[[email protected] ~]# systemctl start httpd

[[email protected] ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago

Docs: man:httpd(8)

man:apachectl(8)

Main PID: 26811 (httpd)

Status: "Processing requests..."

CGroup: /system.slice/httpd.service

├─26811 /usr/sbin/httpd -DFOREGROUND

├─26812 /usr/sbin/httpd -DFOREGROUND

├─26813 /usr/sbin/httpd -DFOREGROUND

├─26814 /usr/sbin/httpd -DFOREGROUND

├─26815 /usr/sbin/httpd -DFOREGROUND

└─26816 /usr/sbin/httpd -DFOREGROUND

Oct 23 00:48:17 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...

Oct 23 00:48:33 Centos7-node2 httpd[26811]: AH00558: httpd: Could not reliably determine the server‘s fully qua...ssage

Oct 23 00:48:43 Centos7-node2 systemd[1]: Started The Apache HTTP Server.

Hint: Some lines were ellipsized, use -l to show in full.

[[email protected] ~]# systemctl reload httpd

[[email protected] ~]# systemctl stop httpd

注意:

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

在loader中第一个disabled表示系统启动是否自启动,为非自动。 第二个不知道干嘛

Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago

Active 表示服务是不是在运行

如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)

[[email protected] ~]# systemctl enable httpd

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

禁用

systemctl disabled httpd

[[email protected] ~]# systemctl disable httpd

Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

使用systemctl命令杀死服务

[[email protected] ~]# systemctl kill httpd

[[email protected] ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago

Docs: man:httpd(8)

man:apachectl(8)

Process: 26901 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)

Main PID: 26869 (code=exited, status=0/SUCCESS)

Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"

Oct 23 00:51:38 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...

Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00557: httpd: apr_sockaddr_info_get() failed for Centos7-node2

Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00558: httpd: Could not reliably determine the server‘s fully qua...ssage

Oct 23 00:52:03 Centos7-node2 systemd[1]: Started The Apache HTTP Server.

Oct 23 00:56:59 Centos7-node2 kill[26901]: kill: cannot find process ""

Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service: control process exited, code=exited status=1

Oct 23 00:56:59 Centos7-node2 systemd[1]: Unit httpd.service entered failed state.

Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service failed.

Hint: Some lines were ellipsized, use -l to show in full.

注意:Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago

这个的failed表示的是kill掉的?

##########################  使用Systemctl控制并管理挂载点 #################3

[[email protected] ~]# systemctl list-unit-files --type=mount

UNIT FILE                     STATE

dev-hugepages.mount           static

dev-mqueue.mount              static

proc-sys-fs-binfmt_misc.mount static

sys-fs-fuse-connections.mount static

sys-kernel-config.mount       static

sys-kernel-debug.mount        static

tmp.mount                     disabled

挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态

systemctl start tmp.mount

systemctl status tmp.mount

systemctl reload tmp.mount

在启动时激活、启用或禁用挂载点(系统启动时自动挂载)

# systemctl is-active tmp.mount

# systemctl enable tmp.mount

# systemctl disable tmp.mount

在Linux中屏蔽(让它不能启用)或可见挂载点

# systemctl mask tmp.mount

ln -s ‘/dev/null‘‘/etc/systemd/system/tmp.mount‘

# systemctl unmask tmp.mount

rm ‘/etc/systemd/system/tmp.mount‘

####################  控制系统运行等级  ##################

启动系统救援模式

# systemctl rescue

Broadcast message from [email protected] on pts/0(Wed2015-04-2911:31:18 IST):

The system is going down to rescue mode NOW!

进入紧急模式

# systemctl emergency

Welcome to emergency mode!After logging in, type "journalctl -xb" to view

system logs,"systemctl reboot" to reboot,"systemctl default" to try again

to boot intodefault mode.

列出当前使用的运行等级

注意:init 1也是可以使用的

# systemctl get-default

multi-user.target

注意:who -r 也是可以查看的

启动运行等级5,即图形模式

# systemctl isolate runlevel5.target

# systemctl isolate graphical.target

启动运行等级3,即多用户模式(命令行)

# systemctl isolate runlevel3.target

# systemctl isolate multiuser.target

设置多用户模式或图形模式为默认运行等级

# systemctl set-default runlevel3.target

# systemctl set-default runlevel5.target

重启、停止、挂起、休眠系统或使系统进入混合睡眠

# systemctl reboot

# systemctl halt

# systemctl suspend

# systemctl hibernate

# systemctl hybrid-sleep

对于不知运行等级为何物的人,说明如下。

Runlevel 0 : 关闭系统

Runlevel 1 : 救援?维护模式

Runlevel 3 : 多用户,无图形系统

Runlevel 4 : 多用户,无图形系统

Runlevel 5 : 多用户,图形化系统

Runlevel 6 : 关闭并重启机器

注意:在centos7 中仍然可以使用init 0 关机 init 6 启动。

时间: 2024-08-01 19:58:57

centos7 中 systemd systemctl管理服务的命令的相关文章

Centos7中systemd的管理

管理服务 安装应用在/etc/systemd/system下会生成一系列应用的配置文件等,系统管理员和用户使用 /usr/lib/systemd/system:发行版打包者使用 时间单位默认是秒,毫秒(ms),分钟(m) service unit file文件组成: [Unit]:描述,依赖关系 Description:描述 After:服务要在After定义的后面的服务启动之后再启动,Before正好相反 Requires:必要的依赖关系,依赖于服务的应用 Wants:弱依赖; Conflic

自定义 systemctl 管理服务

自定义 systemctl 管理服务 ##简介systemd是靠管理unit的方式来控制开机服务,开机级别等功能.在/usr/lib/systemd/system目录下包含了各种unit文件,有service后缀的服务unit,有target后缀的开机级别unit等,这里介绍关于service后缀的文件.因为systemd在开机要想执行自启动,都是通过这些*.service 的unit控制的,服务又分为系统服务(system)和用户服务(user). 系统服务:开机不登陆就能运行的程序(常用于开

CentOS7中搭建DNS域名解析服务

DNS系统的作用 DNS系统在网络中的作用就是维护着一个地址数据库,其中记录了各种主机域名与IP地址的对应关系,以便为客户机提供正向或反向的地址查询服务. 正向解析:根据主机名称(域名)查找对应的IP地址 反向解析:根据IP地址查找对应的主机域名 DNS系统类型 缓存域名服务器 也称为高速缓存服务器 通过向其他域名服务器查询获得域名->IP地址记录 将域名查询结果缓存到本地,提高重复查询时的速度 主域名服务器 特定DNS区域的官方服务器,具有唯一性 负责维护该区域内多有域名->IP地址的映射记

linux CentOS7 中安装包管理:rpm 、yum及源码包安装使用

一. 安装软件包的三种方法 yum ---python rpm工具 yum工具 源码包 二. rpm包介绍 设置光驱并挂载: [[email protected] ~]# mount /dev/cdrom /mntmount: /dev/sr0 写保护,将以只读方式挂载 [[email protected] ~]# ls /mntEULA    isolinux  repodata                      TRANS.TBL GPL     LiveOS    RPM-GPG-

centos7 systemctl 管理 mysql

centos 7 开始使用systemctl 管理服务 服务脚本目录 /etc/systemd/system/mysql.service 脚本如下: # # Simple MySQL systemd service file # # systemd supports lots of fancy features, look here (and linked docs) for a full list: #  http://www.freedesktop.org/software/systemd/

手动编译安装Libvirt之后利用systemctl管理libvirtd服务

因为要给特殊的虚拟机关联文件指定selinux标签,而默认的Libvirt没有这个功能,所以需要修改LIbvirt源代码,重新编译安装Libvirt,而手动编译安装的LIbvirt,没有办法使用systemctl管理libvirtd服务,只能通过libvirtd -d的方式手动启动.然而,手动启动的Libvirtd服务并不符合开发规范,所以只能手动把libvirtd.service添加到systemctl管理. 这就要详解下systemctl管理程序了 一.systemctl介绍 systemc

CentOS7 服务检查命令

列出所有服务:systemctl list-unit-files 列出运行中的服务:systemctl list-units 列出所有失败的服务:systemctl --failed 检查单个服务是否启用:systemctl is-enabled mysqld.service 查看单个服务的状态:systemctl status firewalld.service 启动.重启..停止.重载某个服务 # systemctl start httpd.service# systemctl restar

#25 centos7(RHEL)系列操作系统的启动流程、systemd的特性、与命令systemctl的使用

systemd的新特性: 1.在系统引导的时候可以实现服务的并行启动: 2.能够实现按需激活进程: 在系统启动时,需要随系统启动服务,其服务进程并没有启动,但是systemd为每一个此类服务进程都注册了对应的套接字:我们称这种服务处理方式为"半激活状态": 3.能够对当前系统的用户空间的每个进程状态快照:以后如果进程出现问题或故障,可以迅速恢复进程状态至过去的某一时刻: 4.systemd内部有一种基于依赖关系来定义的服务控制逻辑: 核心管理概念:unit文件 由systemd相关的配

Centos7中systemctl命令详解

LinuxSystemctl是一个系统管理守护进程.工具和库的集合,用于取代System V.service和chkconfig命令,初始进程主要负责控制systemd系统和服务管理器.通过Systemctl –help可以看到该命令主要分为:查询或发送控制命令给systemd服务,管理单元服务的命令,服务文件的相关命令,任务.环境.快照相关命令,systemd服务的配置重载,系统开机关机相关的命令. 1. 列出所有可用单元 # systemctl list-unit-files 2. 列出所有