systemd概述

  1)systemd是一种新的linux系统服务管理器,用于替换init系统,能够管理系统启动过程和系统服务,一旦启动起来,就将监管整个系统。在centos7系统中,PID1被systemd所占用;
  2)systemd可以并行地启动系统服务进程,并且最初仅启动确实被依赖的服务,极大减少了系统的引导时间,这也就是为什么centos7系统启动速度比centos6快许多的原因;
  3)systemctl 是 systemd 的主命令,用于管理系统及服务。

    systemd的新特性:
            系统引导时实现服务并行启动;
            按需激活进程;
            系统状态快照;
            基于依赖关系定义服务控制逻辑

    systemd的关键特性:
            基于socket的激活机制:socket与程序分离;
            基于bus的激活机制;
            基于device的激活机制;
            基于Path的激活机制;
            系统快照:保存各unit的当前状态信息于持久存储设备中;
            向后兼容sysv init脚本,放在/etc/init.d/
    注意:systemctl的命令是固定不变的;非由systemd启动的服务,systemctl无法与之通信

systemd核心概念:unit

    unit由其相关配置文件进行标识、识别和配置;文件中主要包含了系统服务、监听的socket、保存的快照以及其它与init相关的信息; 这些配置文件主要保存在:
            /usr/lib/systemd/system
            /run/systemd/system
            /etc/systemd/system
    
    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, 用于定义文件系统中的一文件或目录

systemd的service unit file详解

1)service unit file 通常由三部分组成:
   [Unit]:定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等;
   [Service]:与特定类型相关的专用选项;此处为Service类型;
   [Install]:定义由“systemctl enable”以及"systemctl disable“命令在实现服务启用或禁用时用到的一些选项
2)各部分释义:
   [Unit] 段的常用选项:
     Description:描述信息; 意义性描述;
     After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动;其功能与Before相反;
     Requies:依赖到的其它units;强依赖,被依赖的units无法激活时,当前unit即无法激活;
     Wants:依赖到的其它units;弱依赖;
     Conflicts:定义units间的冲突关系
   [Service] 段的常用选项:
     Type:用于定义影响ExecStart及相关参数的功能的unit进程启动类型,其类型有:
        simple:默认值,执行ExecStart指定的命令,启动主进程
        forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
        oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
        dbus:当前服务通过D-Bus启动
        notify:当前服务启动完毕,会通知systemd再继续往下执行
        idle:若有其他任务执行完毕,当前服务才会运行
     EnvironmentFile:环境配置文件;
     ExecStart:指明启动unit要运行命令或脚本; 
     ExecStartPre:在ExecStart之前运行;
        ExecStartPost:在ExecStart之后运行;
     ExecStop:指明停止unit要运行的命令或脚本;
     Restart:当设定Restart=1时,则当次daemon服务意外终止后,会再次自动启动。
   [Install] 段的常用选项:
     Alias:别名,可使用systemctl command Alias.service;
     RequiredBy:被哪些units所依赖;
     WantedBy:被哪些units所依赖
注意:对于新创建的unit文件或修改了的unit文件,要通知systemd重载此配置文件,通过命令 systemctl daemon-reload.

实例:编译安装httpd,并实现通过systemd进行管理

1)通过 rpm -q httpd 命令检查系统是否已安装apache,如有先进行卸载;

2)下载Apache(httpd)源码:https://httpd.apache.org/

3)自定义安装依赖APR,需要先下载APR库和PCRE环境:http://apr.apache.org/download.cgi    http://www.pcre.org

4)通过 yum group install "Development Tools" 安装开发工具包

5)解压所有已下载的文件

6)安装PCRE

[root@happiness pcre-8.42]# ./configure --prefix=/usr/local/pcre
[root@happiness pcre-8.42]# make
[root@happiness pcre-8.42]# make install
注意:此处如果安装的是pcre2的版本,后期编译Apache时会出现错误提示:configure: error: Did not find pcre-config script at /usr/local/pcre。
   此时我们需要卸载已安装的pcre2:rm -rf /usr/loca/pcre,直接删除编译安装的目录即可删除pcre。

7)安装APR

[root@happiness apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@happiness apr-1.6.3]# make
[root@happiness apr-1.6.3]# make install

8)安装APR-util

[root@happiness apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@happiness apr-util-1.6.1]# make
[root@happiness apr-util-1.6.1]# make install

9)安装Apache(httpd)

[root@happiness httpd-2.4.33]# ./configure --prefix=/usr/apache24 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre
[root@happiness httpd-2.4.33]# make
[root@happiness httpd-2.4.33]# make install

10)运行httpd服务

[root@happiness httpd-2.4.33]# /usr/apache24/bin/apachectl start

注意:启动httpd服务时有可能提示:servername未设置:

    AH00557: httpd: apr_sockaddr_info_get() failed for happiness
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

  此时我们可通过修改配置文件解决问题:

[root@happiness httpd-2.4.33]# vim /usr/apache24/conf/httpd.conf

  

11)添加服务脚本,使得可以通过systemd来管理httpd服务:

CentOS7服务的systemctl脚本存放在/usr/lib/systemd目录下,有系统(/usr/lib/systemd/system)和用户(/usr/lib/systemd/user)之分。此处我们在/usr/lib/systemd/system目录下新建httpd.service的脚本文件,内容如下:

[Unit]
Description=The Apache HTTP Server 2.4.33
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
EnvironmentFile=/usr/apache24/conf/httpd.conf
ExecStart=/usr/apache24/bin/httpd -k start -DFOREGROUND
ExecReload=/usr/apache24/bin/httpd -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}

[Install]
WantedBy=multi-user.target

此时我们可以通过 systemctl命令来管理httpd服务了。

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!