systemd 的配置文件大部分放置于 /usr/lib/systemd/system/ 目录内。

以audit服务为例

[Unit]
Description=Security Auditing Service
DefaultDependencies=no
## If auditd.conf has tcp_listen_port enabled, copy this file to
## /etc/systemd/system/auditd.service and add network-online.target
## to the next line so it waits for the network to start before launching.
After=local-fs.target systemd-tmpfiles-setup.service
Conflicts=shutdown.target
Before=sysinit.target shutdown.target
RefuseManualStop=yes #RefuseManualStop=yes,是不允许手动停止的
ConditionKernelCommandLine=!audit=0
Documentation=man:auditd(8) https://github.com/linux-audit/audit-documentation

[Service]
Type=forking
PIDFile=/var/run/auditd.pid
ExecStart=/sbin/auditd
## To not use augenrules, copy this file to /etc/systemd/system/auditd.service
## and comment/delete the next line and uncomment the auditctl line.
## NOTE: augenrules expect any rules to be added to /etc/audit/rules.d/
ExecStartPost=-/sbin/augenrules --load
#ExecStartPost=-/sbin/auditctl -R /etc/audit/audit.rules
ExecReload=/bin/kill -HUP $MAINPID
# By default we don't clear the rules on exit. To enable this, uncomment
# the next line after copying the file to /etc/systemd/system/auditd.service
#ExecStopPost=/sbin/auditctl -R /etc/audit/audit-stop.rules

[Install]
WantedBy=multi-user.target

    

[Unit]: 

这个项目内主要在规范服务启动的脚本、环境配置文件文件名、重新启动的方式等等。
[Install]:这个项目就是将此 unit 安装到哪个 target 里面去的意思!

 

[Service] 区块:启动行为

        启动命令
        ExecStart字段:定义启动进程时执行的命令
        ExecReload字段:重启服务时执行的命令
        ExecStop字段:停止服务时执行的命令
        ExecStartPre字段:启动服务之前执行的命令
        ExecStartPost字段:启动服务之后执行的命令
        ExecStopPost字段:停止服务之后执行的命令

        启动类型
        Type字段定义启动类型。它可以设置的值如下:
        simple(默认值):ExecStart字段启动的进程为主进程
        forking:ExecStart字段将以fork()方式启动,此时父进程将会退出,子进程将成为主进程(后台运行)
        oneshot:类似于simple,但只执行一次,Systemd 会等它执行完,才启动其他服务
        dbus:类似于simple,但会等待 D-Bus 信号后启动
        notify:类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务
        idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合

        重启行为
        Service区块有一些字段,定义了重启行为:
        KillMode字段:定义 Systemd 如何停止 sshd 服务:
        control-group(默认值):当前控制组里面的所有子进程,都会被杀掉
        process:只杀主进程
        mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
        none:没有进程会被杀掉,只是执行服务的 stop 命令。
        Restart字段:定义了 sshd 退出后,Systemd 的重启方式

        Restart字段可以设置的值如下。
        no(默认值):退出后不会重启
        on-success:只有正常退出时(退出状态码为0),才会重启
        on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
        on-abnormal:只有被信号终止和超时,才会重启
        on-abort:只有在收到没有捕捉到的信号终止时,才会重启
        on-watchdog:超时退出,才会重启
        always:不管是什么退出原因,总是重启
        注:对于守护进程,推荐设为on-failure。对于那些允许发生错误退出的服务,可以设为on-abnormal。
        RestartSec字段:表示 Systemd 重启服务之前,需要等待的秒数。

    [Install] 区块
    Install区块,定义如何安装这个配置文件,即怎样做到开机启动。
    WantedBy字段:表示该服务所在的 Target。
    Target的含义是服务组,表示一组服务。
    WantedBy=multi-user.target指的是:sshd 所在的 Target 是multi-user.target。
    这个设置非常重要,因为执行systemctl enable sshd.service命令时,sshd.service的一个符号链接,就会放在/etc/systemd/system目录下面的multi-user.target.wants子目录之中。
    Systemd 有默认的启动 Target。

posted on 2022-06-17 11:42  属于我的梦,明明还在  阅读(1766)  评论(0)    收藏  举报