rhcl7的用户空间父进程systemd

Sysmtedcentos

systemd是什么?网络

  你们知道当内核完成初始化,以只读方式挂载rootfs之后,运行了用户空间的第一个进程叫init,咱们以centos系列为视角来看看:socket


        CentOS 5: SysV initide

        CentOS 6:Upstartui

        CentOS 7:Systemdspa



    Systemd的新特性:hibernate

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

                按需激活进程;注:访问时候才激活进程orm

                系统状态快照;进程

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




核心概念:unit

unit是什么?

        Unit 文件统一了过去各类不一样的系统资源配置格式,例如服务的启/停、定时任务、设备自动挂载、网络配置、设备配置、虚拟内存配置等。而 Systemd 经过不一样的经过文件的后缀名来区分这些配置文件,.service 文件即是其中的一种。


文件中主要包含了系统服务、监听的socket、保存的快照以及其它与init相关的信息; 这些配置文件主要保存在:

        /usr/lib/systemd/system  注:系统或用户提供的配置文件

        /run/systemd/system      注:软件运行时生成的配置文件

        /etc/systemd/system      注:系统或第三方软件安装时添加的配置文件


下面是 Systemd 所支持的12种 Unit 文件类型。


        Service unit:文件扩展名为.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没法与之通讯;





    service类型的unit文件命令---syscemctl


用法:

    syscemctl命令:systemctl  [OPTIONS...]  COMMAND  [NAME...]


centos6和7服务启动关闭对比


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

二、重载或重启服务: systemctl  reload-or-restart  NAME.servcie

三、重载或条件式重启服务:systemctl  reload-or-try-restart  NAME.service

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

五、查看全部已激活的服务:systemctl  list-units  --type  service

六、查看全部服务(已激活及未激活): chkconfig --lsit  ==>  systemctl  list-units  -t service     --all 

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

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

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

十、禁止某服务设定为开机自启: systemctl  mask  NAME.service

十一、取消此禁止: systemctl  unmask  NAME.servcie

十二、查看服务的依赖关系:systemctl  list-dependencies  NAME.service




剖析service unit file:


文件一般由三部分组成:

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

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

   [Install]:定义由“systemctl  enable”以及"systemctl  disable“命令在实现服务                         启用或禁用时用到的一些选项;


 Unit段的经常使用选项:

        Description:描述信息; 意义性描述;

         After:定义unit的启动次序;表示当前unit应该晚于哪些unit启动;其功能与Before相反;

         Requies:依赖到的其它units;强依赖,被依赖的units没法激活时,当前unit即没法激活;

         Wants:依赖到的其它units;弱依赖;

         Conflicts:定义units间的冲突关系;



Service段的经常使用选项:

 Type:用于定义影响ExecStart及相关参数的功能的unit进程启动类型;


        simple:

        forking:

        oneshot:

        dbus:

        notify:

        idle:

        EnvironmentFile:环境配置文件;

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

        ExecStop:指明中止unit要运行的命令或脚本;

        Restart:


 Install段的经常使用选项:

                    Alias:

                    RequiredBy:被哪些units所依赖;

                    WantedBy:被哪些units所依赖; 

注意:对于新建立的unit文件或,修改了的unit文件,要通知systemd重载此配置文件;




管理target units:


运行级别:

0  ==>  runlevel0.target,  poweroff.target

1  ==>  runlevel1.target,  rescue.target

2  ==>  runlevel2.tartet,  multi-user.target

3  ==>  runlevel3.tartet,  multi-user.target

4  ==>  runlevel4.tartet,  multi-user.target

5  ==>  runlevel5.target,  graphical.target

6  ==>  runlevel6.target,  reboot.target



centos6和7运行级别切换对比

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

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

三、查看全部级别: systemctl  list-units  -t  target  -a

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

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

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

七、切换至emergency模式: systemctl  emergency




   其它经常使用命令:

关机: systemctl  halt,  systemctl  poweroff

重启: systemctl  reboot

挂起: systemctl  suspend

快照: systemctl  hibernate 

快照并挂起: systemctl  hybrid-sleep

# systemctl  daemon-reload

练习:为当前系统的httpd服务提供一个unit文件;

相关文章
相关标签/搜索