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)
上一篇 2016-09-21 08:59
下一篇 2016-09-21 09:48

相关推荐

  • vim的基本使用

    前言:     Vim是一个类似于Vi的著名的功能强大、高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性。VIM是自由软件。    Vim普遍被推崇为类Vi编辑器中最好的一个,事实上真正的劲敌来自Emacs的不同变体。1999 年Emacs被选为Linuxworld文本…

    Linux干货 2017-07-30
  • 开班第一天

    我的一篇小日记

    Linux干货 2018-03-26
  • 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 答:who | cut -f 1 -d " " | sort -u 2、取出最后登录到当前系统的用户的相关信息。 答:who | tail -n 1 3、取出当前系统上被用户当作其默认shell的最多的那个shell。 答:cut -f7 -d: …

    Linux干货 2016-11-25
  • Linux中的账号管理之命令的使用(中)

    linux中账号管理的命令非常多,我这里主要介绍最常见的几个命令,这些命令分别是针对用户和组的管理 主要介绍对用户管理的命令: 一、用户创建:useradd useradd命令用于Linux中创建的新的系统用户。useradd可用来建立用户帐号。帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号。使用useradd指令所建立的帐号,实…

    Linux干货 2016-08-07
  • 网络管理

    一、网络概念     什么是网络     资源共享的功能和优点         数据和应用程序         资源 &…

    Linux干货 2016-09-09
  • MySQL常见备份与恢复方案

    MySQL常见备份方案有以下三种:        mysqldump + binlog        lvm + binlog          xtrabac…

    Linux干货 2015-10-01