systemd

1、systemd

(1)CentOS 7 使用systemd替换了SysV。Systemd目的是要取代一直在使用的init系统,兼容SysV和LSB的启动脚本,负责在系统启动或运行时,激活系统资源、服务器进程和其它进程。

(2)systemd的新特性:

系统引导是实现服务并行启动
按需启动守护进程
自动化管理各服务间的依赖关系
同时采用sockets式与D-Bus总线式激活服务
系统状态快照与系统恢复

2、unit

(1)systemd引进了unit区块,记录启动顺序与依赖关系。unit表示不同类型的systemd对象,通过配置文件进行表示和配置;文件中主要包含了系统服务、监听sockets、保存的系统快照已经其它与init相关的信息。这些配置文件主要保存在以下目录中:

/usr/lib/systemd/system/目录:此目录保存的是各个服务最主要的启动脚本设置文件,类似于centos7之前的/etc/init.d/目录。
/run/systemd/system/目录:系统执行过程中所产生的服务脚本,优先于上面目录运行。
/etc/systemd/system/目录:管理员建立的执行脚本,类似于/etc/rc.d/rcN.d/S* (N:0-6)的功能,比上面的目录优先运行(最优先)。

(2)unit的常见类型:(systemctl -t help 命令查看unit类型)

systemd

service:文件扩展名为.service,用于定义系统服务
target:文件扩展名为.target,用于模拟实现“运行级别”
snapshot:文件扩展名为.snapshot,用于管理系统快照
device:文件扩展名为.device,用于定义内核识别的设备
mount:文件扩展名为.mount,用于定义文件系统挂载点
automount:文件扩展名为.automunt,用于定义文件系统的自动挂载点
swap:文件扩展名为.swap,用于标识swap设备
path:文件扩展名为.path,用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务,如:spool目录
    [root@localhost system]# ls /var/spool
    abrt  abrt-upload  anacron  at  cron  cups  lpd  mail  plymouth  postfix

(3)unit关键特性:

基于socket 的激活机制:socket 与服务程序分离
基于d-bus 的激活机制:
基于device 的激活机制:
基于path 的激活机制:
系统快照:保存各unit 的当前状态信息于持久存储设备中
向后兼容sysv init 脚本
    不兼容:
    systemctl 命令固定不变,不可扩展
    非由systemd 启动的服务,systemctl无法与之通信

3、systemctl命令用法

systemctl - Control the systemd system and service manager
systemctl [OPTIONS...] COMMAND [NAME...]

注意:以下name.service表示某个具体服务

启动:

systemctl start name.service

停止:

systemctl stop name.service

重启:

systemctl restart name.service

查看状态:

systemctl status name.service

条件式重启:已启动才重启,否则不做操作:

systemctl try-restart name.service

重载或重新启动服务:先加载,再启动:

systemctl reload-or-restart name.service

重载或条件式重启服务:

systemctl reload-or-try-restart name.service

禁止自动和手动启动服务:

systemctl mask name.service

取消禁止:

systemctl unmask name.service

查看某服务当前激活与否的状态:

systemctl is-active name.service

查看所有已经激活的服务:

systemctl list-units --type(-t) service

查看所有的服务(包括激活和未激活):

systemctl list-units --type service -all(-a)

loaded:Unit 配置文件已处理
active(running): 一次或多次持续处理的运行
active(exited): 成功完成一次性的配置
active(waiting): 运行中,等待一个事件
inactive: 不运行
enabled: 开机启动
disabled: 开机不启动
static: 开机不启动,但可被另一个启用的服务激活

设定某服务开机启动:

systemctl enable name.service (相当于chkconfig name on)

设定某服务开机禁止启动:

systemctl disable name.service(相当于chkconfig name off)

查看所有服务的开机自启状态:

systemctl list-unit-files --type service(相当于chkconfig --list)

列出该服务在哪些运行级别下启用和禁用:

ls /etc/systemd/system/*.wants/name.service(列出的为启用)

systemd

查看服务是否开机自启:

systemctl is-enabled name.service

查看服务的依赖关系:

systemctl list-dependencies name.service

杀掉进程:

systemctl kill 进程号(名)

4、其他常用命令

切换至紧急救援模式:

systemctl rescue

切换至emergency 模式:

systemctl emergency

其它常用命令:

传统命令init ,poweroff ,halt ,reboot 都成为systemctl 的软链接
关机:systemctl halt 、systemctl poweroff
重启:systemctl reboot
挂起:systemctl suspend
休眠: :systemctl hibernate
休眠并挂起:systemctl hybrid-sleep

5、target units:运行级别

相关配置文件:.target

ls /usr/lib/systemd/system/*.target:列出所有target unit。
systemctl list-unit-files --type target --all:显示所有target服务

(1)运行级别:

0 ==> runlevel0.target, poweroff.target
1 ==> runlevel1.target, rescue.target
2 ==> runlevel2.target, multi-user.target
3 ==> runlevel3.target, multi-user.target
4 ==> runlevel4.target, multi-user.target
5 ==> runlevel5.target, graphical.target
6 ==> runlevel6.target, reboot.target

(2)查看某级别的依赖性:

systemctl list-dependencies name.target

systemd

(3)级别切换:

systemctl isolate name.target

systemd

注意:只有/lib/systemd/system/name.target 文件中AllowIsolate=yes 才能切换(修改文件需执行systemctl daemon-reload 才能生效)

systemd

(4)查看默认运行级别:

systemctl get-default

systemd

(5)修改默认级别:

systemctl set-default name.target (实质是创建了个指向/usr/lib/systemd/system/目录的软连接)

systemd

systemd

6、service unit文件格式

/etc/systemd/system/:系统管理员和用户使用 
/usr/lib/systemd/system/:发行版打包者使用

我们先看看service unit的文件内容,以httpd.service为例。

[root@localhost system]# cat httpd.service 
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

说明:

以“#”开头的行后面的内容表示注释
相关布尔值,1 、yes 、on 、true  都是开启,0 、no 、off、false  都是关闭。
时间单位默认是秒,所以要用毫秒(ms)分钟(m)等请显示说明

service unit file通常由三部分组成:

[Unit]:定义与unit类型无关的通用选项,用于描述unit类型的描述信息、unit行为及依赖关系等
[Service]:与特定类型相关的专用选项;此处为service类型
[Install]:定义由“systemctl enable”已经“systemctl disable”命令在实现服务启用、禁用时用到的一些选项

Unit段的常用选项:

Description:描述信息
After:定义unit的启动次序,表示当前unit应该晚于那些unit启动,其功能与Before相反
Requires:依赖到的其它units,强依赖,被依赖的units无法激活是,当前的unit也无法激活
wants:依赖到的其它units,弱依赖
Conflicts:定义units间的冲突关系

Service段的常用选项:

Type:定义影响ExecStart及相关参数的功能的unit进程启动类型
    simple :默认值,这个daemon 主要由ExecStart 接的指令串来启动,启动后常驻于内存中
    forking :由ExecStart 启动的程序透过spawns 延伸出其他子程序来作为此daemon 的主要服务。原生父程序在启动结束后就会终止
    oneshot :与simple 类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中
    dbus :与simple 类似,但这个daemon 必须要在取得一个D-Bus的名称后,才会继续运作. 因此通常也要同时设定BusNname= 才行
    notify :在启动完成后会发送一个通知消息。还需要配合NotifyAccess 让来让 Systemd 接收消息
    idle :与simple 类似,要执行这个daemon 必须要所有的工作都顺利执行完毕后才会执行。这类的daemon 通常是开机到最后才执行即可的服务
EnvironmentFile :环境配置文件
ExecStart :指明启动unit 要运行命令或脚本的绝对路径
ExecStartPre:ExecStart 前运行
ExecStartPost:ExecStart 后运行
ExecStop :指明停止unit 要运行的命令或脚本
Restart :当设定Restart=1 时,则当次daemon 服务意外终止后,会再次自动启动此服务

Install段的常用选项:

Alias :别名,可使用systemctl command Alias.service
RequiredBy :被哪些units 所依赖,强依赖
WantedBy :被哪些units 所依赖,弱依赖
Also :安装本服务的时候还要安装别的相关服务

注意:对于新创建的unit 文件,或者修改了的unit 文件,要通知systemd 重载此配置文件,可用命令: systemctl daemon-reload

7、示例:创建一个备份服务

(1)创建一个脚本,用于被创建的服务调用

[root@localhost system]# cat /testdir/bak.sh 
#!/bin/bash
# 备份/etc/目录
tar -Jcvf /testdir/etc-`date +%F`.tar.xz /etc/ &> dev/null

(2)给bak.sh脚本添加执行权限

[root@localhost system]# chmod u+x /testdir/bak.sh

(3)创建bak.service服务

[root@localhost system]# pwd
/etc/systemd/system (创建服务的文件存放位置)
[root@localhost system]# cat bak.service 
[Unit]
Description=backup my etc
Requires=atd.service
[Service]
Type=simple
ExecStart=/bin/bash -c "echo /testdir/bak.sh|at now"
#使用/bin/bash命令来执行bak.sh脚本,因此第2步可以省略
[Install]
WantedBy=multi-user.target

(4)启用服务

[root@localhost system]# systemctl daemon-reload
[root@localhost system]# systemctl start bak.service

(5)验证

systemd

原创文章,作者:pao,如若转载,请注明出处:http://www.178linux.com/48172

(2)
paopao
上一篇 2016-09-21
下一篇 2016-09-21

相关推荐

  • 《 Git 权威指南 》学习笔记

    这个没办法直接发博客了,篇幅太长,无法直接在 Blog 上排版了,直接发上 pdf 文档了。 接 《LNAMP Shell 部署脚本》 博文的内容,线上测试系统的 Git 环境部署。 整个 Git 环境包括了:     Git / Gitweb     Gitolite / Gerrit &nbsp…

    Linux干货 2015-10-27
  • python Django分页

    自定义html_helper.py  —>  Page_helper类 #coding:utf-8 from django.utils.safestring import mark_safe class Page_Helper(object):    …

    Linux干货 2016-08-22
  • iptables初探

    iptables 简述 基础知识 命令使用 简述 iptables是什么?netfilter又是什么? iptables是位于用户控件的一个防火墙规则控制管理工具。netfilter是位于内核中的真正的防火墙,由五个钩子函数(hooks)而组成。 iptables的作用是什么? 用来添加,删除,管理netfilter规则。 netfilter的作用是什么? …

    2016-05-31
  • Redis高可用架构(1)—Keepalive+VIP

    最近整理一下Redis高可用架构的文档,也准备分享出来,虽然这些架构也不是很复杂。Redis的高可用方案目前主要尝试过5种方式,其中2种方式已经在线上使用。 1)Redis Master-Slave + Keepalive + VIP。这是很经典的db架构,也可以用与mysql的主从切换。基本原理是:Keepalive通过脚本检测master的存活,然后通过…

    Linux干货 2016-04-13
  • http协议简介

        http:Hyper Text Transfer Protocol,超文本传输协议。是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。1960年美国人Ted Nelson构思了一种通过计算机处理文本信息的方法,并称之为超文本(hyp…

    Linux干货 2015-11-18
  • ansible部署KeepAlived动态站点

    一、设计原因 利用ansible可以自动化部署KeepAlived 的双方模型(其中包括两个动态的站点),多组服务器需要一样的部署时 可以利用ansible写的程序来代替重复性的操作。 二、设计拓扑结构及服务器详情   三、详细步骤 1、环境的搭建 (1)安装ansible,同时配置私钥免密码进行通信 [root@localhost ~]# ssh…

    2015-05-13