systemd总结

1.systemd

CentOS 7 使用systemd替换了SysV。Systemd目的是要取代Unix时代以来一直在使用的init系统,兼容SysV和LSB的启动脚本,而且够在进程启动过程中更有效地引导加载服务。

systemd的特性有:

    ps:使用 systemctl 控制单元时,通常需要使用单元文件的全名,包括扩展名(例如 sshd.service)。但是有些单元可以在systemctl中使用简写方式。)

  • 支持并行化任务

  • 同时采用socket式与D-Bus总线式激活服务;

  • 按需启动守护进程(daemon);

  • 利用 Linux 的 cgroups 监视进程;

  • 支持快照和系统恢复;

  • 维护挂载点和自动挂载点;

  • 各服务间基于依赖关系进行精密控制。

  • 1.启动单元:存放在 /usr/lib/systemd/system/ 和 /etc/systemd/system/ 目录(后者优先级更高)。

  • 2.在systemd中所有的服务功能都叫被称为一个单元。

  • eg:

    • 系统服务(.service):用户自己的应用服务也存放在里面,如mysql

    • 挂载点(.mount)

    • sockets(.sockets)

    • 系统设备、交换分区/文件、启动目标(target)、文件系统路径、计时器

    • 如果无扩展名,systemctl 默认把扩展名当作 .service。例如 netcfg 和 netcfg.service 是等价的。

    • 挂载点会自动转化为相应的 .mount 单元。例如 /home 等价于 home.mount。

    • 设备会自动转化为相应的 .device 单元,所以 /dev/sda2 等价于 dev-sda2.device。

a)命令:

1:开机启动单元
$:systemctl  
(和$:systemctl list-units)

2.失败启动
$:systemctl --failed

3.激活单元
$:systemctl start <单元>

4.立即停止
$:systemctl stop <单元>

5.重启单元
$:systemctl reload <单元>

6.运行状态
$:systemctl status <单元>

############自动启动##########
7.是否为自动启动
$:systemctl is-enabled <单元>

8.设置为自动启动
$:systemctl enable <单元>

9.去掉开机启动
$:systemctl disable <单元>

###############重启systemctl########
$:systemctl daemon-reload

    b)系统管理

1.重启
$:systemctl reboot

2.退出并关闭电影
$:systemctl poweroff

3.待机
$:systemctl suspend

4.休眠
$:systemctl hibernate

5.混合休眠模式(同时休眠到硬盘并待机)
$:systemctl hybrid-sleep

    c)自己编写service

启动方式
编写自定义的service文件时,可以选择几种不同的服务启动方式。启动方式可通过配置文件 [Service] 段中的 Type= 参数进行设置。具体的参数说明请参阅 man systemd.service 。

  • Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。

  • Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。

  • Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。

  • Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。

  • Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。

       A unit configuration file whose name ends in .service encodes information about a process

       controlled and supervised by systemd.

一个单元配置文件,名字以服务名字开始,以.service结束。里面保存了一个可以被systemd控制的进程。

       This man page lists the configuration options specific to this unit type. See systemd.unit(5)

       for the common options of all unit configuration files. The common configuration items are

       configured in the generic "[Unit]" and "[Install]" sections. The service specific configuration

       options are configured in the "[Service]" section.

这篇文章列举了单元配置选项。查看system.unit(5)可以查看单元配置文件的公共选项。配置文件的公共选项

包括了[unit]和[install]部分。自定义service定义配置选项是在[service]

       Additional options are listed in systemd.exec(5), which define the execution environment the

       commands are executed in, and in systemd.kill(5), which define the way the processes of the

       service are terminated, and in systemd.resource-control(5), which configure resource control

       settings for the processes of the service.

增加选项可以是使用systemd.exec(5)列出来,如果定义一个运行环境命令。使用systemd.kill(5)来关闭一个

服务

       Unless DefaultDependencies= is set to false, service units will implicitly have dependencies of

       type Requires= and After= on basic.target as well as dependencies of type Conflicts= and

       Before= on shutdown.target. These ensure that normal service units pull in basic system

       initialization, and are terminated cleanly prior to system shutdown. Only services involved

       with early boot or late system shutdown should disable this option.

  除非默认依赖设置为false,服务但与将设置为 Requires= and After= basic.target ,依赖类型 Conflicts= 

  and Before= shutdown.target。这些普通服务单元将在系统初始化的initialization后启动,在系统关闭前

  关闭。当服务在系统启动前启动、或者在系统关闭后关闭,就需要关闭这个选项。

       If a service is requested under a certain name but no unit configuration file is found, systemd

       looks for a SysV init script by the same name (with the .service suffix removed) and

       dynamically creates a service unit from that script. This is useful for compatibility with

       SysV. Note that this compatibility is quite comprehensive but not 100%. For details about the

       incompatibilities see the Incompatibilities with SysV[1] document.

  如果一个服务有一个确认的名字,但是单元配置文件没有发现被找到,sytemd将回去查询SysV的相同名字

  的初始化脚本(SysV的启动文件在 /etc/rc.d/里面),

  然后根据脚本动态创建一个.service 脚本(并不会直接创建一个.service)。这是很好的兼容了SysV,

  但是需要注意的是,这并不能100%兼容SysV.详细的请查看SysV的文档。

目标(target)
启动级别(runlevel)是一个旧的概念。现在,systemd 引入了一个和启动级别功能相似又不同的概念——目标(target)。不像数字表示的启动级别,每个目标都有名字和独特的功能,并且能同时启用多个。一些目标继承其他目标的服务,并启动新服务。systemd 提供了一些模仿 sysvinit 启动级别的目标,仍可以使用旧的 telinit 启动级别 命令切换。

mysql的linux安装

1、centos7直接安装默认的mariadb

猜你喜欢

转载自my.oschina.net/u/2246410/blog/1802704