systemd简介

1、简述systemd的新特性

        RedHat 系的Linux系统,init进程的有:

        CentOS 5 : SysV-->init

        CentOS 6 : Upstart-->init

        CentOS 7: Systemd

其中,Systemd 的新特性有:

        系统引导时实现进程并行启动;

        可以按需启动进程;也可以根据依赖性进行服务控制;

        系统状态快照;

2、Unit (单元)

Systemd 是通过unit 及其配置文件来管理控制系统服务、监听的socket、快照及其他与init相关的信息的。

unit 配置文件通常放置于:

          /usr/lib/systemd/system 、/etc/systemd/system、/run/systemd/system

unit的常见类型:

        Service unit:  配置文件以.service 结尾,用于定义系统服务

        Target unit: 以.target 结尾, 用于模拟运行级别

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

        Socket unit:  以.socket 结尾, 用于定义进程间通讯的socket文件

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

        Mount unit : 以.mount 结尾,用于定义系统挂载点

        Automount unit: 以.automount结尾, 用于定义系统的自动挂载点

        Swap unit :  以.swap 结尾, 用于标识swap设备;

        Snapshot unit : 以.snapshot 结尾, 用于管理系统快照;

比如, service unit 配置文件 内容如下:  

>>>> cat /usr/lib/systemd/system/firewalld.service

[Unit]

Description=firewalld - dynamic firewall daemon

Before=network-pre.target

Wants=network-pre.target

After=dbus.service

After=polkit.service

Conflicts=iptables.service ip6tables.service ebtables.service ipset.service

Documentation=man:firewalld(1)

[Service]

EnvironmentFile=-/etc/sysconfig/firewalld

ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS

ExecReload=/bin/kill -HUP $MAINPID

# supress to log debug and error output also to /var/log/messages

StandardOutput=null

StandardError=null

Type=dbus

BusName=org.fedoraproject.FirewallD1

KillMode=mixed

[Install]

WantedBy=multi-user.target

Alias=dbus-org.fedoraproject.FirewallD1.service

从上可以看出, unit 配置文件一般分为三个部分:

        unit : 定义unit 的依赖性、帮助、描述等信息

                    Description : unit 意义描述

                    Before: 定义当前unit应该先于哪些unit 启动;

                    After: 定义当前unit 应该晚于哪些unit 启动;

                    Wants:定义当前unit 依赖哪些units ; 弱依赖,即其他units 可有可无;

                   Conflicts: 定义冲突的units 

                    Documentation: 帮助信息

        service:定义unit 的相关选项

                    EnvironmentFile: 定义unit 环境变量信息

                    ExecStart:指明启动unit 时运行的脚本

                    ExecStop:指明停止unit 时运行的脚本

                    ExecReload:指明重载unit时运行的脚本

                    Type:Configures the unit process startup type that affects the functionality of ExecStart and related options. /  配置影响ExecStart及相关选项的 unit进程启动类型

                            simple – The process started with ExecStart is the main process of the service./默认选项,ExecStart 启动的进程是主进程;

                            forking – The process started with ExecStart spawns a child process that becomes the main process of the service. The parent process exits when the startup is complete./ExecStart 启动的进程会启动一个子进程,子进程将会成为服务的主进程。父进程在服务启动完成之后退出.

                            oneshot – This type is similar to simple, but the process exits before starting consequent units./ 类似于simple,但是这个进程在启动后续的unit之前就退出了.

                            dbus – This type is similar to simple, but consequent units are started only after the main process gains a D-Bus name. /类似于simple,但是只有该主进程收到一个D-Bus名称后才会启动后续units .

                            notify – This type is similar to simple, but consequent units are started only after a notification message is sent via the sd_notify() function. / 类似simple,后续的units 只有在主进程收到sd_notify()函数的通知后才会启动;

                            idle – similar to simple, the actual execution of the service binary is delayed until all jobs are finished, which avoids mixing the status output with shell output of services./ 服务的二进制程序被延迟执行,直到所有的任务完成,以避免状态输出与服务器的shell 输出混合;


         install :This section is not interpreted by systemd(1) during runtime. It is used exclusively by the enable and disable commands of the systemctl(1) tool during installation of a unit./这部分在运行期间是不会被执行。它只在执行systemctl enable|disable时使用;


3、实现systemd 管理niginx 服务

        3.1 将nginx 源码包安装至/usr/local/nginx 下;

       3.2 创建unit配置文件

             >>> cat /usr/lib/systemd/system/nginx.service

            [Unit]

            Description=nginx

            [Service]

           ExecStart=/usr/local/nginx/sbin/nginx

            ExecStop=/usr/local/nginx/sbin/nginx -s stop

            ExecReload=/usr/local/nginx/sbin/nginx -s reload

            Type=forking

        

你可能感兴趣的:(systemd简介)