systemd和systemctl详解

简介:

一、Systemd
二、systemctl
三、service的unit文件格式


systemd与systemctl
一、Systemd:
    POST-->Boot sequence(BIOS)-->Boot loader(MBR)-->kernel(ramdisk)-->rootfs(swtich-root)-->/sbin/init
    所有用户空间的进程,都由init负责,当需要调用特权指令的时候,才会切换到内核
init
    CentOS 5:SysV init //真正的bell实验室
    CentOS 6:Upstart //引用ubuntu的二次发行版
    CentOS 7:Systemd //模仿MAC OS的
Systemd的新特性:
    1.系统引导时实现服务并行启动
    2.按需激活进程//开机后进入需要启动的程序,会进入半激活状态,第一次访问的时候会直接执行,
        //例如http会占用80端口,直到有用户访问
    3.系统状态快照 //用户空间,可以回滚到
    4.基于依赖关系定义服务控制逻辑
    
核心概念:unit
    由其相关的配置文件进程标识和识别和配置
    文件中主要包含了系统服务,监听的socket,保存的快照以及其他与init相关的信息
    这些配置文件保存在:
        /usr/lib/systemd/system/    //每一个文件都可以被称为一个unit
        /run/systemd/system
        /etc/systemd/system
[root@MT ~]# systemctl disable named
    Removed symlink /etc/systemd/system/multi-user.target.wants/named.service.
[root@MT ~]# systemctl enable named
    Created symlink from /etc/systemd/system/multi-user.target.wants/named.service to /usr/lib/systemd/system/named.service.
    
unit类别:/usr/lib/systemd/system    

1
2
3
4
5
6
7
8
9
10
11
     *.service 用于定义系统服务;类似于service start|stop|restart等的脚本,相当于以前的/etc/init.d/*
     *.target  用于模拟实现 "运行级别" ,因为CentOS7默认是没有运行级别的,主要是为了兼容
     *.device 用于定义内核识别的设备,
                 //之前是udev根据/sys目录下内核所探测到的输出的信息创建的/dev,
                 //CentOS7设备主要由systemd[主打]和udev创建,systemd用于识别硬件 ,基于*.device识别
     *.mount 定义fs挂载点,    
     *.socket 用于标识进程间通信用到的socket文件,由systemd负责
     *.snapshot 管理系统快照
     *.swap 用于标识swap设备,
     *.automount ,fs的自动挂载点设备  //例如U盘自动挂载点
     *.path  用于定义fs中的一个文件或目录,如果不存在,systemd会自动创建

    
    mount中有大量的cgroup//内核进行资源分配,docker主要依赖于namespace和cgroup技术

systemd关键特性:
    基于socket的激活机制;socket与程序分离;//先把socket分配给一个服务,但是该服务先不启动
    基于bus的激活机制,如果总线上有对该服务的请求,就基于总线的方式激活该服务
    基于device的激活机制,当某个设备插入的时候,激活mount unit和automount unit,自动挂载至某挂载点,挂载点不存在,将自动创建。监控内核输出的硬件信息,
    基于path的激活机制,监控某个文件或目录是否存在,激活相应进程
        //例如某进程异常停止,丢下lockfile,systemd会发起进程,提示用户报告bug等
    系统快照,保存个unit的当前状态,到持久存储中,systemd基于unit工作的
        //可以回到过去,异常关机
    向后兼容sysv init的脚本
        /dev/init.d/* 可以受systemd控制
    
    不兼容:
        systemctl的命令是固定不变的;支持的start|stop|restart...等命令是固定的,不能变
        非由systemd启动的服务,systemctl无法与之通信
    管理系统服务:
        CentOS7:service类型的unit文件来实现管理
            兼容:/etc/init.d/*

二、systemctl:控制systemd和service
    systemctl [options...] command [name...]
    
    COMMANDS:
        启动:systemctl start NAME.service ==> service NAME start
        停止:systemctl stop NAME.service ==> service NAME stop
        重启:systemctl restart NAME.service ==> service NAME resatart
        状态:systemctl status NAME.service ==> service NAME status
        条件式重启://如果服务之前是启动的,就重启,否则就算了
            systemctl try-restart NAME.service ===> service NAME conresart
        重载服务或重启服务:systemctl reload-or-restart NAME.service //支持重载就重载,不支持重载,就重启
        重载或条件式重启:systemctl reload-or-try-restart NAME.service
        
        查看某服务当前激活与否的状态:systemctl is-active NAME.service //或者 /etc/systemd/system/multi-user.target.wants/named.service 
        查看所有已经激活的服务:systemctl list-units --type service //--type也可以用-t
        查看所有服务(包括未激活的):systemctl list-units -t service -a ==> chkconfig --list  //-a:--all
        
        设置开机自启与关闭:
            systemctl enable|disable NAME.service ===> chkconfig NAME on|off
        查看某服务是否能开机自启:
            chkconfig --list NAME ==> systemctl is-enabled NAME.service
        禁止/关闭 设置某服务开机自启
            systemctl mask NAME.service 
            systemctl unmask NAME.service
        查看服务的依赖关系
            systemctl list-dependencies httpd.service 
        
    systemctl status httpd.service
        loaded:loaded ( ......  ; disabled ) //loaded:表示unit文件已经被systemctl装载了,受systemctl管理
                                            //disabled:表示没有开机启动
        Active: inactive(dead) //状态为停止
管理target units://用于模拟之前的运行级别
    运行级别:

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

    systemd poweroff|reboot 
    systemd rescue|multi-user    
    
    级别切换:
        init # | systemctl isolate NAME.target
    查看级别:
        runlevel,who -r ==> systemctl list-units -t target
                        ==> systemctl list-units -t target -a ///查看所有级别
    获取默认运行级别
        systemctl get-default
        systemctl set-default NMAE.target 
        
        cat /etc/inittab //CentOS6针对该配置文件
    切换至紧急救援模式:systemctl rescue //CentOS6的话会执行/etc/rc.d/rc.sysinit
    切换到emergency模式 不会执行/etc/rc.d/rc.sysinit ,额外驱动都不会被装载
            systemctl emergency
其他常用命令
    关机:systemctl halt|poweroff
    重启:systemctl reboot
    挂起:systemctl suspend
    快照:systemctl hibernate //hibernate:
    快照并挂起:sytemctl hybrid-sleep //混合睡眠

三、service的unit文件格式
    ls /etc/systemd/system/default.target -l //链接到 /usr/lib/sytemd/system/multi-user.target
    [root@MT system]#  systemctl set-default graphical.target
        Removed symlink /etc/systemd/system/default.target.        ///删除etc下的default的链接,然后换位graphical.target
        Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
    
配置文件:
    /etc/systemd/system
    /run/systemd/system
    /usr/lib/sytemd/system
    
cat /usr/lib/systemd/system/httpd.service

1
2
3
4
5
     [Unit]  //定义与Unit类型相关的通用选项;用于提供unit的描述信息,unit行为及依赖关系等
     Description=The Apache HTTP Server   //描述信息
     After=network.target remote-fs.target nss-lookup.target  //依赖关系
     Documentation=man:httpd(8)
     Documentation=man:apachectl(8)
1
2
3
4
5
6
7
8
9
10
11
12
13
     [Service]     //unit类型,Service|Target|Mount|...,与特定类型相关的专用选项,此处为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 w
     ant# 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

    
 

1
2
3
4
5
6
7
8
9
    [Install]     //定义由systemctl enable或systemctl disable命令再实现服务是否能够开机启用或禁用的选项
     WantedBy=multi-user.target    
+++++++++++++++++++++++++++++++++++++++++++++++++++++++        
     Unit段的常用选项
         Description:描述信息,意义性描述
         After:unit的启动次序,隐含有依赖关系,当前unit应该晚于哪些unit启动,其功能与before相反
         Requirts:强依赖:依赖到的其他units,必须有的依赖于其他unit启动
         Wants:弱依赖:依赖到的其他unit,可有可无的依赖于其他unit
         Conflicts:冲突,定义units之间的冲突关系

    Service的常用选项
  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
       Type:定义影响ExecStart及相关参数的功能与unit进程启动的类型
             类型  //一个服务启动的进程,有主进程和子进程之分
                 simple:默认,ExecStart启动的进程为主进程
                 forking: 由ExecStart生成的一个子进程将成为主进程
                         //可能会生成很多子进程,但是之后一个是主进程
                         //选定主进程后,父进程会退出
                 oneshot:  //一锤定音,在后续的unit启动之前,该进程会退出,类似于simple
                 dbus: //类似于simple,后续的进程在得到dbus名称之后,才能启动
                 notify: //类似于simple,获取到notify通知后,才运行该命令
                 idle:  //类似于simple,
         EnvironmentFile 环境配置文件,加载环境变量,或者变量
         ExecStart  //指明启动该unit要运行的命令或脚本;ExecStartPre,ExecStartPost        
         ExecReload 
         ExecStop   //指明停止unit要运行的命令或者脚本
         KillSignal
         PrivateTmp
         Restart:  //进程关闭后,会自动重启

    Install的常用选项
        Alias:
        RequiredBy:被那些units所依赖
        WantedBy:被那些units所依赖
            
注意:对于新创建的unit文件,或者修改了的unit文件
    要通知sysemd 重载此配置文件
    systemctl daemon-reload //所有具有守护进程的服务,重载
    











本文转自MT_IT51CTO博客,原文链接:http://blog.51cto.com/hmtk520/1981433,如需转载请自行联系原作者

相关文章
开机启动
开机启动
103 0
|
Linux
Linux:systemctl、service、chkconfig
Linux:systemctl、service、chkconfig
105 0
|
运维 网络协议 关系型数据库
Systemd | 学习笔记
快速学习Systemd,掌握如何进行服务的管理和配置,并引导学生主动完成服务的编写,为后续的运维工作打下基础
Systemd | 学习笔记