一,systemd的用途?

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

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

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

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

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

systemctl是systemd的管理控制工具

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

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

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

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

二,查看systemctl所属的包

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

三,查看systemctl的版本和帮助

1,查看版本

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

2,查看帮助

  1. [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的一个符号链接

  1. [root@blog systemd]# ll / | grep lib
  2. lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib
  3. 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,原始文档请参见:

  1. [root@centos8 ~]# man systemd
  2. ...
  3. The following unit types are available:
  4. 1. Service units, which start and control daemons and the processes they consist of. For details, see systemd.service(5).
  5. 2. Socket units, which encapsulate local IPC or network sockets in the system, useful for socket-based activation. For details about socket units,
  6. see systemd.socket(5), for details on socket-based activation and other forms of activation, see daemon(7).
  7. 3. Target units are useful to group units, or provide well-known synchronization points during boot-up, see systemd.target(5).
  8. 4. Device units expose kernel devices in systemd and may be used to implement device-based activation. For details, see systemd.device(5).
  9. 5. Mount units control mount points in the file system, for details see systemd.mount(5).
  10. 6. Automount units provide automount capabilities, for on-demand mounting of file systems as well as parallelized boot-up. See
  11. systemd.automount(5).
  12. 7. Timer units are useful for triggering activation of other units based on timers. You may find details in systemd.timer(5).
  13. 8. Swap units are very similar to mount units and encapsulate memory swap partitions or files of the operating system. They are described in
  14. systemd.swap(5).
  15. 9. Path units may be used to activate other services when file system objects change or are modified. See systemd.path(5).
  16. 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
  17. management purposes. See systemd.slice(5).
  18. 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    查询服务的状态

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

可以看到状态是active

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

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

#stop: 用来停止服务

  1. [root@centos8 ~]# systemctl stop firewalld

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

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

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

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

2,启动firewalld服务

#start 用来启动服务

  1. [root@centos8 ~]# systemctl start firewalld

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

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

3,禁止firewalld开机自启动

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

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

#disable  用来禁止开机自启动

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

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

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

4,实现firewalld开机自启动

#eable  用来实现开机自启动

  1. [root@centos8 ~]# systemctl enable firewalld
  2. Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service /usr/lib/systemd/system/firewalld.service.
  3. 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  判断是否开机自启动

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

5,重新启动firewalld服务

  1. [root@centos8 ~]# systemctl restart firewalld

6,注销cups服务

#mask用来注销服务,

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

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

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

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

系统都会给出提示已经masked

7,取消注销cups服务

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

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

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

  1. [root@centos8 ~]# systemctl list-units

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

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

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

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

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

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

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

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

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

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

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

针对四种state的说明:

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

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

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

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

8,查看unit文件

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

  1. [root@blog ~]# systemctl cat openresty.service
  2. # /usr/lib/systemd/system/openresty.service
  3. [Unit]
  4. Description=The OpenResty Application Platform
  5. After=syslog.target network.target remote-fs.target nss-lookup.target
  6.  
  7. [Service]
  8. Type=forking
  9. PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
  10. ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
  11. ExecStart=/usr/local/openresty/nginx/sbin/nginx
  12. ExecReload=/bin/kill -s HUP $MAINPID
  13. ExecStop=/bin/kill -s QUIT $MAINPID
  14. PrivateTmp=true
  15.  
  16. [Install]
  17. WantedBy=multi-user.target

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

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

  1. [root@blog ~]# systemctl daemon-reload

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

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

也可只看其中的一项:

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

九,service文件的格式说明:

1,看一个例子:

# openresty的service文件

  1. [root@centos8 ~]# more /usr/lib/systemd/system/openresty.service
  2. [Unit]
  3. Description=The OpenResty Application Platform
  4. After=syslog.target network.target remote-fs.target nss-lookup.target
  5.  
  6. [Service]
  7. Type=forking
  8. PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
  9. ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
  10. ExecStart=/usr/local/openresty/nginx/sbin/nginx
  11. ExecReload=/bin/kill -s HUP $MAINPID
  12. ExecStop=/bin/kill -s QUIT $MAINPID
  13. PrivateTmp=true
  14.  
  15. [Install]
  16. 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的版本

  1. [root@blog ~]$ cat /etc/redhat-release
  2. 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. Spring Boot 启动第一个页面(Spring二)

    在前面的文章里我配置好了spring boot的环境,并选择STS开发工具 具体文章见:https://blog.csdn.net/qq_38175040/article/details/105481 ...

  2. oracle之三rman 备份

    rman 备份 7.1 归档方式下rman备份常用语法: 7.1.1 backup 备份 1)备份全库:1.1 RMAN> backup database format='/u01/myrman ...

  3. css常用属性之绝对定位、相对定位、滚动条属性、背景图属性、字体、鼠标、超链接跳转页面

    1.绝对定位position: fixed(比如广告页面向下滑动的时候,页面最上方有个标题不能随之滑动,就需要用到position: fixed,同时还需要用到一个标签(标签高度很高才会出现滚动的情况 ...

  4. git如何在远程某个分支的基础上新建分支

    1.任意新建文件夹,右击git bash here $ git init(将此目录变成本地仓库) 2.$ git remote add origin 'https://git............g ...

  5. 回归 | js实用代码片段的封装与总结(持续更新中...)

      上一次更博还是去年10月28号了,截至今天已经有整整4个月没有更新博客了,没更新博客不是代表不学了,期间我已经用vue做了两个项目,微信小程序做了一个项目,只是毕竟找到工作了,想偷偷懒,你懂的. ...

  6. spring framework源码之AnnotationConfigApplicationContext

    AnnotationConfigApplicationContext 内部使用了AnnotatedBeanDefinitionReader:ClassPathBeanDefinitionScanner ...

  7. 1.8HDFS细节

  8. python爬取虎牙直播颜值区美女主播照片

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 python免费学习资 ...

  9. Guava Cache详解

    适用性 缓存在很多场景下都是相当有用的.例如,计算或检索一个值的代价很高,并且对同样的输入需要不止一次获取值的时候,就应当考虑使用缓存 Guava Cache与ConcurrentMap很相似,但也不 ...

  10. Processing 网格纹理制作(棋盘格)

    写在前面的话 很久没有写博文了.最近在整理Processing有关文档,看到之前做的一些例子,想着分享在互联网上,当然和以前一样,目前也仅为了给初学者有个学习参考,笔者能力有限.废话不多说,干就完事了 ...