Linux Simple Systemd Service Guide

Simple Systemd Service Guide

主题

  • Systemd介绍
  • Systemd基本操作
  • 怎样编写_service_.service文件
  • 怎样部署service

Systemd介绍

  • Linux OS的系统和服务管理者
  • 兼容SysV初始化脚本(init scripts)
  • 提供如下功能特性:
    • 系统启动时系统服务的并行启动
    • Daemon程序的按需激活
    • 支持系统状态快照(snapshots)
    • 基于依赖的服务控制逻辑
  • systemd取代RHL7的Upstart,作为默认的init系统

Systemd基本操作

  • 启动服务

    systemctl start name.service
  • 停止服务

    systemctl stop name.service
  • 重启服务

    systemctl restart name.service
  • 重载配置

    systemctl reload name.service
  • 检查服务状态

    systemctl status name.service

  • 输出所有服务状态

    systemctl list-units --type service --all
  • 使能服务

    systemctl enable name.service
  • 禁止服务

    systemctl disable name.service
  • 列出所有服务并检查是否使能

    systemctl list-unit-files --type service

怎样编写name.service文件

name.service文件组成

  • [Unit]
    • 包含一般的选项,不依赖于单元类型(unit type)
    • 选项:提供单元描述,指定单元行为,设置对其他单元的依赖关系
  • [unit type]
    • 如果一个单元有特定的指令,则这些指令被组合在以单元类型命名的块下。
      比如,service单元文件包含在[Service]块中。
  • [Install]
    • 包含systemctl enable和disable命令使用的单元安装信息

重要的[Unit]块选项

  • Description

    单元有意义的描述信息,systemctl status命令输出中显示该文本信息。

  • Documentation

    提供该单元的URI引用文件列表。

  • After

    定义单元的启动顺序。该单元只在After指定的单元处于激活状态后才启动。

    Before选择与After功能相反。

  • Requires

    配置与其他单元的依赖关系。在Requires中的单元与该单元一起激活。

    如果任何需要的单元启动失败,这个单元就不会被激活。

  • Wants

    配置比Requires弱的依赖关系。

    如果任何列出的单元没有成功启动,也不会影响该单元的激活。

    建议用于建立定制单元依赖关系。

  • Conflicts

    配置反向的依赖关系,即与Requires配置相反的依赖。

重要的[Service]块选项

  • Type

    配置单元启动类型。影响ExecStart和其他相关选项的功能。

  • simple

    默认选项。ExecStart启动的进程为服务的主进程。

  • forking

    ExecStart启动的进程创建一个子进程作为服务的主进程。启动完成后父进程退出。

  • oneshot

    与simple相似,但是在启动后续的单元之前进程退出。

  • dbus

    与simple相似,但是仅在主进程得到D-bus名称之后才启动后续的单元。

  • notify

    与simple相似,但是仅在通过sd_notify()函数发送通知消息后才启动后续的单元。

  • idle
    与simple相似,但是直到所有job完成后,才真正执行服务的二进制程序,这避免与服务shell的状态输出信息混淆。

  • ExecStart

    当单元启动时,执行该选项指定的命令或脚本。

  • ExecStop

    当单元停止时,执行该选项指定的命令或脚本。

  • ExecReload

    当单元重装时,执行该选项指定的命令或脚本。

  • Restart

    如果该选项使能,当服务进程退出时,服务重启。作为systemctl stop命令的异常处理。

  • RemainAfterExit

    如果该选项设置为True,即使服务所有的进程退出,这个服务也被认为处于激活状态(active)。
    默认是False。
    当Type=oneshort配置时,这个选项有特殊的用处。

重要的[Install]块选项

  • Alias

    提供单元的额外名称,以空格隔开。除了systemctl enable,大部分sytemctl命令可以使用别名带去真正的单元名称。

  • RequiredBy

    依赖于该单元的其他单元列表。
    当该单元使能时,RequiredBy中的单元就会生成依赖于该单元的Require依赖关系。

  • WantedBy

    弱依赖于该单元的其他单元列表。
    当该单元使能时,WantedBy中的单元就会生成依赖于该单元的Want依赖关系。

  • Also

    指定和该单元一起安装或卸载的其他单元列表。

  • DefaultInstance

    限定的实例化单元。即该选项指定了该单元使能的默认实例。
    Limited to instantiated units, this option specifies the default instance for which the unit is enabled.

例子:自定义中断绑核服务

[Unit]
Description=Configure common_affinity service for system!
After=network.target

[Service]
Type=oneshot
ExecStart=/etc/init.d/common_affinity
ExecStop=/home/zxuss/affinity/superstop.sh
RemainAfterExit=True

[Install]
WantedBy=multi-user.target

怎样部署service

以自定义中断绑核服务为例子:

1.将name.service文件拷贝到/usr/lib/systemd/system/目录下
cp -f ${work_path}/common_affinity.service /usr/lib/systemd/system/

2.将服务启动脚本拷贝到/etc/init.d/目录,或者其他目录
cp -f ${work_path}/common_affinity /etc/init.d/

3.如果已经部署了name.service服务,则先禁止该服务,然后重新使能该服务
systemctl disable common_affinity.service
systemctl reenable common_affinity.service

4.如果没有部署过name.service服务,则直接使能name.service服务
systemctl enable common_affinity.service

参考文档:

Managing Services with systemd

systemd.service — Service unit configuration

How To Use Systemctl to Manage Systemd Services and Units

您的支持是对博主最大的鼓励,感谢您的认真阅读。

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利

猜你喜欢

转载自www.cnblogs.com/smith9527/p/10300561.html