大多数Linux发行版使用 systemd作为系统服务管理工具。

systemctl是systemd的主要命令,用于管理控制服务。

这篇文章中将介绍如何创建允许你使用systemctl命令的sysytemd服务文件,如何无需reboot来重启systemd并reload unit文件,如何enable 一个新的服务。

并举例介绍大多数重要的systemd服务文件选项。

创建 systemd service 文件

创建systemd service file /etc/systemd/system/name.service (name 是示例名称,根据你自己的服务名称自行替换)

准备使用自定义服务可执行文件。这可以是自定义创建的脚本,也可以是软件提供商提供的可执行文件。如果需要,准备一个PID文件以保存自定义服务主进程的常量PID。还可以包含环境文件来存储服务的shell变量。确保源脚本是可执行的(通过执行)而不是交互式的。

touch /etc/systemd/system/name.service
chmod 664 /etc/systemd/system/name.service

打开该文件,添加systemd最小服务配置选项:

[Unit]
Description=service_description
After=network.target [Service]
ExecStart=path_to_executable
Type=forking
PIDFile=path_to_pidfile [Install]
WantedBy=default.target

其中:

  • service_description 是一个信息性描述,显示在日志日志文件和systemctl status命令的输出中。
  • 该设置确保仅在网络运行后启动服务。添加以空格分隔的其他相关服务或目标列表 After
  • path_to_executable 代表实际服务可执行文件的路径
  • Type=forking用于进行fork系统调用的守护进程。使用 path_to_pidfile 中指定的PID创建服务的主进程。查找其他启动类型

service 文件发生变更,systemd配置需要重新加载:

$ sudo systemctl daemon-reload

现在,应可以使用 start, stop, restartstatus:

$ sudo systemctl start name
$ sudo systemctl stop name
$ sudo systemctl restart name
$ systemctl status name

在系统启动时自动配置服务,使用 enable

$ sudo systemctl enable name

检查 service 日志:

$ journalctl -u name

Systemd Service File Options 选项

Systemd service files通常含有三个区域。

通用配置项在一般在[Unit][Install]配置

特定于服务的配置选项在 [Service]中进行配置

Important Section Options[Unit]

Option Description
Description unit的简短描述
Documentation 参考文档URI列表
Before, After units 的启动顺序
Requires 如果激活此unit,则此处列出的units也将被激活。如果其他units中的一个停用或发生故障,则此unit将被停用。
Wants 配置较Requires弱的依赖性。如果列出的任何unit未成功启动,则对此unit激活没有影响。这是建立自定义unit依赖关系的推荐方法。
Conflicts 如果一个 unit 已经在其他unit上设置,那么启动前者将停止后者,反之亦然。

表1:Unit Option
> 在大多数情况下,仅使用`After`和`Before` unit 文件选项设置排序依赖关系就足够了。 如果还使用 `Wants`(推荐)或`Requires`设置需求依赖关系,则仍需要指定排序依赖关系。 这是因为排序和需求依赖性彼此独立地工作。

使用 man 查看[Unit]查看完整的选项:

$ man systemd.unit

Important Section Options[Install]

Option Description
Alias 空格分隔的 unit 附加名称列表。大多数命令(不包括systemctl systemctl enable)可以使用别名而不是实际的 unit 名称
RequiredBy, WantedBy 当列出的服务被启动,此服务也会被启动。 参考Wants Requires [Unit]
Also 指定用户运行systemctl enable systemctl disable时要与此 unit 一起启用或禁用的 unit 列表

使用 man 查看[Install]查看完整的选项:

$ man systemd.unit

Important Section Options[Service]

Option Description
Type 配置影响ExecStart功能和相关选项的 unit 进程启动类型。
+ simple - 默认值。从ExecStart开始的进程是服务的主要进程
+ forking - 从ExecStart开始的进程产生了一个子进程,该进程成为该服务的主要进程。启动完成后,父进程退出。
+ oneshot - 类似于simple, 但进程在启动后续units 之前会退出
+ dbus - 类似于simple,但只有在主进程获得D-Bus名称后才启动后续unit
+ notify - 类似于simple,但只有在通过 sd_notify() 函数发送通知消息后才会启动后续unit
+ idle - 类似于simple,服务二进制文件的实际执行被延迟,直到所有作业完成,这避免了将状态输出与服务的shell输出混合。
ExecStart 指定启动设备时要执行的命令或脚本。ExecStartPreExecStartPost指定要在ExecStart之前和之后执行的自定义命令。Type=oneshot可以指定多个自定义命令,然后按顺序执行。
ExecStop 指定 unit 停止时要执行的命令或脚本。
ExecReload 指定重新加载 unit 时要执行的命令或脚本
Restart 启用此选项后,服务将在其进程退出后重新启动,但systemctl命令将执行干净停止( clean stop )
RemainAfterExit 如果设置为True,即使退出所有进程,该服务也会被视为活动状态。默认值为False。如果配置了Type=oneshot,则此选项特别有用

使用 man 查看[Service]查看完整的选项:

$ man systemd.service

postfix.service Unit File 例子

下面是 postfix 包的 systemd 服务文件(/usr/lib/systemd/system/postfix.service redhat OS):

[Unit]
Description=Postfix Mail Transport Agent
After=syslog.target network.target
Conflicts=sendmail.service exim.service [Service]
Type=forking
PIDFile=/var/spool/postfix/pid/master.pid
EnvironmentFile=-/etc/sysconfig/network
ExecStartPre=-/usr/libexec/postfix/aliasesdb
ExecStartPre=-/usr/libexec/postfix/chroot-update
ExecStart=/usr/sbin/postfix start
ExecReload=/usr/sbin/postfix reload
ExecStop=/usr/sbin/postfix stop [Install]
WantedBy=multi-user.target

更多信息,参考 arch 文档:systemd

Systemd: Service File Examples的更多相关文章

  1. .NET Worker Service 部署到 Linux 作为 Systemd Service 运行

    上一篇文章我们了解了如何将.NET Worker Service 作为 Windows 服务运行,今天我接着介绍一下如何将 Worker Service 部署到 Linux 上,并作为 Systemd ...

  2. systemd service

    Man page systemd.unit SYSTEMD.UNIT(5) systemd.unit SYSTEMD.UNIT(5) NAME systemd.unit - Unit configur ...

  3. CENTOS/RHEL 7 系统中设置SYSTEMD SERVICE的ULIMIT资源限制

    遇到的问题: golang程序一直出现 too many open files的报错, 尽管对 /etc/security/limits.conf 做了设置, 对最大文件打开数,最大进程数做了调优. ...

  4. 【转】CENTOS/RHEL 7 系统中设置SYSTEMD SERVICE的ULIMIT资源限制

    在bash中,有个ulimit命令,提供了对shell及该shell启动的进程的可用资源控制.主要包括打开文件描述符数量.用户的最大进程数量.coredump文件的大小等. 在centos 5/6 等 ...

  5. systemd.service 中文手册

    版权声明 本文译者是一位开源理念的坚定支持者,所以本文虽然不是软件,但是遵照开源的精神发布. 无担保:本文译者不保证译文内容准确无误,亦不承担任何由于使用此文档所导致的损失. 自由使用:任何人都可以自 ...

  6. Linux Simple Systemd Service Guide

    Simple Systemd Service Guide 主题 Systemd介绍 Systemd基本操作 怎样编写_service_.service文件 怎样部署service Systemd介绍 ...

  7. systemd service 设置limit,不生效问题

    参考博文: http://smilejay.com/2016/06/centos-7-systemd-conf-limits/(解决方法参考此博文)   问题简述:Centos7下修改系统的最大文件打 ...

  8. 如何编写一个Systemd Service(转)

    转自 https://segmentfault.com/a/1190000014740871 0x01 什么是Systemd Service Systemd 服务是一种以 .service 结尾的单元 ...

  9. Ubuntu18.04初始的systemd service

    Ubuntu18.04初始的systemd service 两个位置 /etc/systemd/system root@dev2:~# ls /etc/systemd/system aliyun.se ...

随机推荐

  1. iOS-MBProgressHUD框架使用(转)

    MBProgressHUD是一个开源类库,实现了各种样式的提示框, 下载地址:https://github.com/jdg/MBProgressHUD,然后把两个MBProgressHUD.h和MBP ...

  2. Jmeter 逻辑控制器 之 Include Controller

    一.认识 Include Controller Include Controller :译为包含控制器,用来添加 Test Fragment(测试片段).具体是什么意思呢,我们先来了解下 Test F ...

  3. flask上下文管理相关-LocalStack 对象维护栈

    LocalStack 对象维护栈 模拟 import threading """ storage = { 1232: {stack:[123,456]} } " ...

  4. 剑指offer 67. 字符串转换为整数(Leetcode 8. String to Integer (atoi))

    题目:剑指offer 67题 需要考虑的情况:空指针.nullptr.空字符串"".正负号.数值溢出.在写代码的时候对这些特殊的输入都定义好合理的输出.可以定义一个全局布尔型变量g ...

  5. Leetcode之并查集专题-684. 冗余连接(Redundant Connection)

    Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...

  6. Git速成学习第五课:分支管理策略

    Git速成学习笔记整理于廖雪峰老师的官网网站:https://www.liaoxuefeng.com/ 通常合并分支时,如果可能用Fast forward模式,但是在这种模式下,删除分支后,会丢掉分支 ...

  7. springBoot--组合注解RestController,GetMapping,PostMapping

    一.RestController @RestController 是@Controller和@ResponseBody的缩写 二.@getMapping和PostMapping @GetMapping ...

  8. last 和 lastb 命令

    NAME last - show listing of last logged in users 数据源:/var/log/wtmp 文件 lsstb - show listing of last l ...

  9. [转帖]Oracle 起诉 Google 事件

    Oracle 起诉 Google 事件 https://www.cnblogs.com/panchanggui/p/9449842.html Oracle 是世界第二大软件公司 世界第一大DBMS公司 ...

  10. tabs 导航 及内容切换

    <!-- 导航头 --> <div class="col-md-6" style="padding: 0px"> <ul id=& ...