Linux自学笔记——Centos7系统之systemd

简介:

 

回顾:

centos系统的启动流程:POST àboot sequeue(BIOS)à bootloader(mbr) à kernel(ramdisk)àrootfs à/sbin/init

init

centos5:sysv init

      centos6:upstart

      centos7:systemd

system的新特性:

      系统引导时实现服务并行启动;

      按需激活进程;

      系统状态快照;

      基于依赖关系定义服务控制逻辑;

核心概念:unit

      unit由其相关配置文件进行标识、识别和配置;文件中主要包含了系统服务、监听的socket、保存的快照以及其他与init相关的信息;这些配置文件主要保存在:

      /usr/lib/systemd/system

      wKioL1nKFLzi3l2ZAAB6EgKffwI151.png-wh_50

      /run/systemd/system

      wKiom1nKFQCgUPQRAAAcHRMTxqI686.png-wh_50

      /etc/systemd/system

      wKiom1nKFQez9NMSAABfh7rOhyE875.png-wh_50

unit的常见类型:

      service init:文件扩展名为.service,用于定义系统服务;

      target unit:文件扩展名为.target,用于模拟实现“运行级别”;

      device unit:.device,用于定义内核识别的设备;

      mount unit:.mount,用于定义文件系统挂载点;

      socket unit:.socket,用于标识进程间通信用到的socket文件;

      snapshot unit:.snapshot,管理系统快照;

      swap unit:.swap,用于标识swap设备;

      automount unit :.automount,文件系统自动点设备;

      path unit:.path,用于定义文件系统中的文件或目录;

关键特性:

      基于socket的激活机制:socket与程序分离;

      基于bus的激活机制;

      基于device的激活机制;

      基于path的激活机制;

      系统快照:保存各unit的当前状态信息于持久存储设备中;

      向后兼容sysv init脚本;

           /etc/init.d

不兼容:

      Systemctl的命令时固定不变的;

      非由systemd启动的服务,systemctl无法与之通信;

管理系统服务:

Centos7:service类型的unit文件

systemctl命令:

      - Control the systemd system and servicemanager

      systemctl [OPTIONS...]  COMMAND  [NAME...]

           启动:service NAME start ==> systemctl startNAME.service

           停止:service NAME stop ==> systemctl stopNAME.service

           重启:service NAME restart ==> systemctlrestart NAME.service

           状态:service NAME status ==> systemctlstatus NAME.service

           Centos6

           wKioL1nKFNKDZNRTAABJJXlLn38736.png-wh_50

           Centos7

           wKiom1nKFRfyMWArAAFw-cjKLfs910.png-wh_50

           条件式重启:service  NAME condrestart  ==>  systemctl try-restart  NAME.service

           重载或重启服务:systemctl reload-or-restart  NAME.service

           重载或条件式重启服务:systemctl reload-or-try-restartNAME.service

 

           查看某服务当前激活与否的状态:systemctl is-activeNAME.service

           wKioL1nKFOLCXwflAAANccErXhs380.png-wh_50

           查看所有已激活的服务:systemctl list-units --typeservice

           wKioL1nKFOnSuJkXAACNvlDhuCo616.png-wh_50

           查看所有服务(已激活及未激活):chkconfig  --list ==> systemctl list-units -t service--all

           wKiom1nKFS7Q7QX7AACWwMFQTuM652.png-wh_50

 

           设置服务开机自启:chkconfig  NAME on ==> systemctl enable NAME.service

           禁止服务开机自启:chkconfig  NAME off ==> systemctl diable NAME.service

           查看某服务是否能开机自启:chkconfig –list NAME ==>systemctl is-enabled NAME.service

           wKiom1nKFTXwZg-PAABta4grNLM624.png-wh_50

          

           查看服务的依赖关系:systemctl list-dependenciesNAME.service

           wKioL1nKFQGDZlskAABPjZMfGX0002.png-wh_50

管理target units

      运行级别:

           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

           7 ==> runlevel6.target,reboot.target

      级别切换:init N ==> systemctl isolate NAME.target

     

      查看级别:runlevel ==> systemctl list-units–type target

      wKiom1nKFUbwkB31AACnqByq4cE622.png-wh_50

      查看所有级别:systemctl list-units -t target –a

      wKiom1nKFU7ThJknAADpdRGtubk697.png-wh_50

      获取默认运行级别:systemctl get-default

      修改默认运行级别:systemctl set-default NAME.target

      wKioL1nKFRrif2V-AAAmExBqp90887.png-wh_50

      切换至紧急救援模式:systemctl rescue

      切换至emergency模式:systemctlemergency

 

其它常用命令:

      关机:systemctl halt,systemctlpoweroff

      重启:systemctl reboot

      挂起:systemctl suspend

      快照:systemctl hibernate

      快照并挂起:systemctl hybrid-sleep

 

Serviceunit file

      文件通常由三部分组成:

           [unit]:定义与unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等;

           [service]:与特定类型相关的专用选项;此处为service类型;

           [install]:定义由“systemctlenable”以及“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 类似,但这个damon必须要在取得一个D-bus的名称后,才会继续运作,因此通常也要同时设定BusNname才行

                      notify

                      idle

           EnvironmentFile:环境配置文件;

           ExecStart:指明启动unit要运行的命令或脚本;ExeStartPre,ExecStartPost

           Restart

      Install段的常用选项:

           Alias:别名可使用systemctl command alias.service

           RequiedBy:被哪些units所依赖;

           WantedBy:被哪些units所依赖;

Note:对于新创建的unit文件或修改了的unit文件,要通知systemd重载配置文件;

      #systemctl daemon-reload

 


本文转自 claude_liu 51CTO博客,原文链接:http://blog.51cto.com/claude666/1968905,如需转载请自行联系原作者

相关文章
|
7天前
|
资源调度 JavaScript 搜索推荐
Linux系统之部署envlinks极简个人导航页
【4月更文挑战第11天】Linux系统之部署envlinks极简个人导航页
41 2
|
10天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
61 0
|
10天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
26 0
|
8天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
25 6
|
3天前
|
运维 网络协议 Unix
18.系统知识-Linux常用命令
18.系统知识-Linux常用命令
|
10天前
|
缓存 Linux
linux centos7 挂载本地iso yum源
linux centos7 挂载本地iso yum源
51 0
|
监控 关系型数据库 应用服务中间件
|
11天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令
|
11天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
8天前
|
NoSQL Linux Shell
常用的 Linux 命令
常用的 Linux 命令
30 9