一,systemd的用途?

Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置

Systemd 的优点是功能强大,使用方便,

缺点是体系庞大,非常复杂

在centos6中,系统的启动管理使用initd,

从centos7开始,systemd成为了系统的默认启动守护进程

systemctl是systemd的管理控制工具

附上systemd的架构图:(来自网络)

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,查看systemctl所属的包

[root@blog ~]$ whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz [root@blog ~]$ rpm -qf /usr/bin/systemctl
systemd-239-13.el8_0.5.x86_64

三,查看systemctl的版本和帮助

1,查看版本

[root@blog ~]$ systemctl --version
systemd 239
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT
+UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4
+SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2
default-hierarchy=legacy

2,查看帮助

[root@blog ~]$ systemctl --help 

四,systemd的核心概念unit是什么?

在systemd中,unit表示不同类型的systemd对象,通过配置文件进行标识和配置;

文件中主要包含了系统服务、监听socket、保存的系统快照以及其它与init相关的信息

五,Systemd中Unit 文件的存放目录

1,

Unit 文件按照 Systemd 约定,被放置指定的三个系统目录之一。

三个目录是有优先级的,如下所示,越上面的优先级越高。

在三个目录中有同名文件的时候,只有优先级最高的目录里的文件会被使用。

  • /etc/systemd/system:系统或用户自定义的配置文件

  • /run/systemd/system:软件运行时生成的配置文件

  • /usr/lib/systemd/system:系统或第三方软件安装时添加的配置文件。

2,

Systemd 默认从目录 /etc/systemd/system/ 读取配置文件,

里面存放的大部分文件都是符号链接,指向目录 /usr/lib/systemd/system/

说明1:

centos8中,/usr/lib/systemd/system 也是lib/systemd/system

因为 /lib目录已经变成了 /usr/lib的一个符号链接

[root@blog systemd]# ll / | grep lib
lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64

说明2:

/etc/systemd/system目录下通常不放置unit文件本身,

而是放置/usr/lib/systemd/system中unit文件的符号链接

六,unit的类型

1,所有类型

Service unit:系统服务

Target unit:多个 Unit 构成的一个组

Device Unit:硬件设备

Mount Unit:文件系统的挂载点

Automount Unit:自动挂载点

Path Unit:文件或路径

Scope Unit:不是由 Systemd 启动的外部进程

Slice Unit:进程组

Socket Unit:进程间通信的 socket

Swap Unit:swap 文件

Timer Unit:定时器

snapshot Unit:表示由 systemctl snapshot 命令创建的 Systemd Units 运行状态快照

2,原始文档请参见:

[root@centos8 ~]# man systemd
...
The following unit types are available:
1. Service units, which start and control daemons and the processes they consist of. For details, see systemd.service(5).
2. Socket units, which encapsulate local IPC or network sockets in the system, useful for socket-based activation. For details about socket units,
see systemd.socket(5), for details on socket-based activation and other forms of activation, see daemon(7).
3. Target units are useful to group units, or provide well-known synchronization points during boot-up, see systemd.target(5).
4. Device units expose kernel devices in systemd and may be used to implement device-based activation. For details, see systemd.device(5).
5. Mount units control mount points in the file system, for details see systemd.mount(5).
6. Automount units provide automount capabilities, for on-demand mounting of file systems as well as parallelized boot-up. See
systemd.automount(5).
7. Timer units are useful for triggering activation of other units based on timers. You may find details in systemd.timer(5).
8. Swap units are very similar to mount units and encapsulate memory swap partitions or files of the operating system. They are described in
systemd.swap(5).
9. Path units may be used to activate other services when file system objects change or are modified. See systemd.path(5).
10. Slice units may be used to group units which manage system processes (such as service and scope units) in a hierarchical tree for resource
management purposes. See systemd.slice(5).
11. Scope units are similar to service units, but manage foreign processes instead of starting them as well. See systemd.scope(5).

七,例子一:管理服务的常用命令:

1,停止firewalld服务

# status    查询服务的状态

[root@centos8 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-04-07 17:22:23 CST; 2min 0s ago
Docs: man:firewalld(1)
Main PID: 1125 (firewalld)
Tasks: 2 (limit: 23837)
Memory: 31.6M
CGroup: /system.slice/firewalld.service
└─1125 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid 4月 07 17:22:22 centos8 systemd[1]: Starting firewalld - dynamic firewall daemon...
4月 07 17:22:23 centos8 systemd[1]: Started firewalld - dynamic firewall daemon.

可以看到状态是active

#is-active 只返回是否在运行中

[root@centos8 ~]# systemctl is-active firewalld
active

#stop: 用来停止服务

[root@centos8 ~]# systemctl stop firewalld

#再次查询状态:注意已变成了inactive

[root@centos8 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2020-04-07 17:24:34 CST; 3s ago
Docs: man:firewalld(1)
Process: 1125 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 1125 (code=exited, status=0/SUCCESS) 4月 07 17:22:22 centos8 systemd[1]: Starting firewalld - dynamic firewall daemon...
4月 07 17:22:23 centos8 systemd[1]: Started firewalld - dynamic firewall daemon.
4月 07 17:24:33 centos8 systemd[1]: Stopping firewalld - dynamic firewall daemon...
4月 07 17:24:34 centos8 systemd[1]: Stopped firewalld - dynamic firewall daemon.

#is-active   只返回是否在运行中

[root@centos8 ~]# systemctl is-active firewalld
inactive

2,启动firewalld服务

#start 用来启动服务

[root@centos8 ~]# systemctl start firewalld

#is-active 只返回是否在运行中

[root@centos8 ~]# systemctl is-active firewalld
active

3,禁止firewalld开机自启动

#is-enabled  判断是否开机自启动

[root@centos8 ~]# systemctl is-enabled firewalld
enabled

#disable  用来禁止开机自启动

[root@centos8 ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

#is-enabled  判断是否开机自启动

[root@centos8 ~]# systemctl is-enabled firewalld
disabled

4,实现firewalld开机自启动

#eable  用来实现开机自启动

[root@centos8 ~]# systemctl enable firewalld
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.

可以看到:使开机自启动,systemctl就是给service文件在/etc/systemd/system/multi-user.target.wants目录下创建了符号链接

#is-enabled  判断是否开机自启动

[root@centos8 ~]# systemctl is-enabled firewalld
enabled

5,重新启动firewalld服务

[root@centos8 ~]# systemctl restart firewalld

6,注销cups服务

#mask用来注销服务,

[root@centos8 ~]# systemctl mask cups
Created symlink /etc/systemd/system/cups.service → /dev/null.

说明:可以看到它的操作是把cups服务创建成为了/dev/null的符号链接

看是否还可以做enable和start操作?

[root@centos8 ~]# systemctl enable cups
Failed to enable unit: Unit file /etc/systemd/system/cups.service is masked. [root@centos8 ~]# systemctl start cups
Failed to start cups.service: Unit cups.service is masked.

系统都会给出提示已经masked

7,取消注销cups服务

[root@centos8 ~]# systemctl unmask cups
Removed /etc/systemd/system/cups.service.

八,例子二:对所有unit的操作

1,列出所有ative的unit,注意:列出的unit是显示是否active

[root@centos8 ~]# systemctl list-units

说明:systemctl 与 systemctl list-units功能相同

2,列出所有unit,包括inactive状态的

[root@centos8 ~]# systemctl list-units --all

3,列出所有未运行的unit,即状态是inactive的

[root@blog ~]# systemctl list-units --all --state=inactive 

4,列出当前活动状态(active)的所有服务单元

[root@centos8 ~]# systemctl list-units --type=service

5,列出所有状态(包括active和inactive)的所有服务unit

[root@centos8 ~]# systemctl list-units --type=service --all

6,列出所有的unit文件,注意:这里显示是否enabled

[root@centos8 ~]# systemctl list-unit-files

针对四种state的说明:

enabled:已建立自启动链接
disabled:没建立自启动链接
static:该配置文件没有[Install]部分(无法执行),只能作为其他配置文件的依赖
masked:该配置文件被禁止建立启动链接

7,列出一个服务所依赖的单元

#list-dependencies 列出一个 Unit 的所有依赖

[root@centos8 ~]# systemctl list-dependencies firewalld.service 

8,查看unit文件

#cat : 用来指定要查看的unit文件

[root@blog ~]# systemctl cat openresty.service
# /usr/lib/systemd/system/openresty.service
[Unit]
Description=The OpenResty Application Platform
After=syslog.target network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target

9,重新加载所有修改过的unit文件

#daemon-reload : 当我们手动修改/添加 unit文件后,需要执行daemon-reload

[root@blog ~]# systemctl daemon-reload

10,显示指定unit的所有底层参数

[root@blog ~]# systemctl show openresty.service 

也可只看其中的一项:

[root@blog ~]# systemctl show -p PIDFile openresty.service
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid

九,service文件的格式说明:

1,看一个例子:

# openresty的service文件

[root@centos8 ~]# more /usr/lib/systemd/system/openresty.service
[Unit]
Description=The OpenResty Application Platform
After=syslog.target network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target

2,[Unit]区块

[Unit]区块通常是配置文件的第一个区块,用来定义 Unit 的元数据,以及配置与其他 Unit 的关系

Description:简短描述

Documentation:文档地址

Requires:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败.(强依赖)

Wants:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败.(弱依赖)

BindsTo:与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行

Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动

After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动

Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行

Condition...:当前 Unit 运行必须满足的条件,否则不会运行

Assert...:当前 Unit 运行必须满足的条件,否则会报启动失败

3,[Install]通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动

WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中(弱依赖)

RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中(强依赖)

Alias:当前 Unit 可用于启动的别名

Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit

4,[Service]区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块

Type:定义启动时的进程行为它有以下几种值

Type=simple:默认值,执行ExecStart指定的命令,启动主进程

Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出

Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行

Type=dbus:当前服务通过D-Bus启动

Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行

Type=idle:若有其他任务执行完毕,当前服务才会运行

ExecStart:启动当前服务的命令

ExecStartPre:启动当前服务之前执行的命令

ExecStartPost:启动当前服务之后执行的命令

ExecReload:重启当前服务时执行的命令

ExecStop:停止当前服务时执行的命令

ExecStopPost:停止当其服务之后执行的命令

RestartSec:自动重启当前服务间隔的秒数

Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括always(总是重启)、on-success、on-failure、on-abnormal、on-abort、on-watchdog

TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数

Environment:指定环境变量

PrivateTmp: 否使用私有的tmp目录

十,查看centos的版本

[root@blog ~]$ cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)

十一,systemctl管理系统的运行级别

参见

centos8使用systemd/systemctl管理系统/服务的更多相关文章

  1. Systemed systemctl 创建服务 详解

    原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html 一.由来 历史上,Linux 的启动一直采用init进 ...

  2. systemd&systemctl

    systemd is a system and service manager for Linux operating systems. When run as first process on bo ...

  3. CentOS7使用systemctl添加自定义服务

    一.简介 Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos 5 6已经是实质差别.systemd是靠管理unit的方式来控制开机服务,开机级别等功能. 在/u ...

  4. 编写使用systemctl启动服务脚本

    CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,还是存在系统服务里吧,即:/usr ...

  5. 使用systemctl管理服务

    系统服务,开机不需要登录就能运行的程序(相当于开机自启)/usr/lib/systemd/system 用户服务,需要登录后才能运行的程序/usr/lib/systemd/user 目录下又存在两种类 ...

  6. 二进制编译安装nginx并加入systemctl管理服务

    一.安装nginx所需环境 # yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y 二.安装ngi ...

  7. Linux_CentOS 中systemctl 管理服务、防火墙 firewalld 以及 SELinux 配置

    使用 systemctl 管理服务 systemctl 就是 service 和 chkconfig 这两个命令的整合,在 CentOS 7 就开始被使用了,systemctl是系统服务管理器命令,它 ...

  8. Linux学习-透过 systemctl 管理服务

    透过 systemctl 管理单一服务 (service unit) 的启动/开机启动与观察状态 一般来说,服务的启动有两个阶段,一 个是『开机的时候设定要不要启动这个服务』, 以及『你现在要不要启动 ...

  9. 使用 Centos 7 的 systemctl 管理服务

    CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,存系统服务里即:/usr/lib/ ...

随机推荐

  1. Mysql主从分离与双机热备超详细配置

    一.概述 本例是在Windows环境,基于一台已经安装好的Mysql57,在本机安装第二台Mysql57服务. 读完本篇内容,你可以了解到Mysql的主从分离与双机热备的知识,以及配置期间问题的解决方 ...

  2. VMware参数disk.EnableUUID生效扫描不出来UUID

    问题描述:搭建RAC中的共享磁盘,在一个节点上部署磁盘,另一个节点加入共享磁盘,使用扫描UUID的方法,但是莫名其妙一直扫描不出来UUID,使用了各种各样的方法,一个星期时间接近崩溃,又搞了一下午的测 ...

  3. 记一次 node 项目重构改进

    摘要:经常听到有祖传的代码一说,就是一些项目经过了很长时间的维护,经过了很多人之手,业务逻辑堆叠的越来越多,然后就变成了一个越来越难以维护. 经常听到有祖传的代码一说,就是一些项目经过了很长时间的维护 ...

  4. selenium中Xpath标签定位和cssSelectors定位(优先用cssSelectors)

    二者的区别:xpath 支持角标定位,cssselector不支持 1.XPath是XML的路径语言,通俗一点讲就是通过元素的路径来查找到这个标签元素. xpath支持属性定位,无论是默认属性还是自定 ...

  5. 乔悟空-CTF-i春秋-Web-SQL

    2020.09.05 是不是有些题已经不能做了--费了半天,到最后发现做不出来,和网上大神的方法一样也不行,最搞笑的有个站好像是别人运营中的,bug好像被修复了-- 做题 题目 题目地址 做题 尝试简 ...

  6. [LeetCode]64. 最小路径和(DP)

    题目 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4 ...

  7. 【深入理解Linux内核架构】6.6 资源分配

    一段摘自<Linux设备驱动程序>的话: 每种外设都通过读写寄存器进行控制.大部分外设都有多个寄存器,不管是内存地址空间还是I/O地址空间,这些寄存器的访问地址都是连续的. 在硬件层,内存 ...

  8. 关于java基础知识的面试题(一)

    1) Java中能否使用goto? 在C/C++中,goto常被用于跳出多重循环.但goto 语句的使用往往会使程序的可读性降低,所以 Java 不允许 goto 跳转.实际上,自从“goto有害论” ...

  9. archaius(3) 配置管理器

    基于上一节介绍的配置源,我们来继续了解配置管理器.配置源只是抽象了配置的获取来源,配置管理器是基于配置源的基础上对这些配置项进行管理.配置管理器的主要功能是将配置从目标位置加载到内存中,并且管理内存配 ...

  10. System Verilog随笔(1)

    测试文件该怎么写? 首先看一个简单代码案例: `timescale 1ns/10ps //1 module test;   //2 intput wire[15:0] a; output reg[15 ...