systemd service unit

本文于2018年1月4号发布在个人博客中,因为个人博客关闭,全部迁移到CSDN,以下是正文:


想要自己的服务像下面这样吗?

systemctl enable your.service
systemctl disable your.service
systemctl start your.service
systemctl stop your.service
systemctl restart your.service
systemctl status your.service

只需要编写一个systemd service的配置文件即可,今天来谈一谈如何编写自己的systemd service配置文件

配置文件格式

配置文件由多个section组成,每个section由多个配置项组成,切section名和配置项名大小写敏感。示例如下:

[Section]
key1=value1
key2=value2
......

sections

systemd service 配置文件有如下section:

  • Unit
  • Service
  • Install
Unit

[Unit]区块通常是配置文件的第一个区块,用来定义 Unit 的元数据,以及配置与其他 Unit 的关系。它的主要字段如下:

Description:简短描述

Documentation:文档地址

Requires:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败

Wants:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败

BindsTo:与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行

Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动

After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动

Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行

Condition...:当前 Unit 运行必须满足的条件,否则不会运行

Assert...:当前 Unit 运行必须满足的条件,否则会报启动失败
Service

[Service]区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块。它的主要字段如下:

  • Type:定义启动时的进程行为。它有以下几种值。
    • Type=simple:默认值,执行ExecStart指定的命令,启动主进程
    • Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
    • Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
    • Type=dbus:当前服务通过D-Bus启动
    • Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
    • Type=idle:若有其他任务执行完毕,当前服务才会运行
  • ExecStart:启动当前服务的命令
  • ExecStartPre:启动当前服务之前执行的命令
  • ExecStartPost:启动当前服务之后执行的命令
  • ExecReload:重启当前服务时执行的命令
  • ExecStop:停止当前服务时执行的命令
  • ExecStopPost:停止当其服务之后执行的命令
  • RestartSec:自动重启当前服务间隔的秒数
  • Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括always(总是重启)、on-success、on-failure、on-abnormal、on-abort、on-watchdog
  • TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
  • Environment:指定环境变量
Install

[Install]通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动。它的主要字段如下:

  • WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
  • RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中
  • Alias:当前 Unit 可用于启动的别名
  • Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit

示例

systemd service配置文件:

[root@localhost ~]# systemctl cat hello.service 
# /usr/lib/systemd/system/hello.service
[Unit]
Description=systemd service unit demo
Documentation=https://sample.com

[Service]
ExecStart=/usr/local/bin/hello

[Install]
WantedBy=multi-user.target

[Service]下的ExecStart指定service启动命令,指向了另外一个脚本,我们来看看内容:

[root@localhost ~]# cat /usr/local/bin/hello 
#!/bin/bash
echo "Hello, Systemd! @ $(date)" > /home/test.log

更新完systemd service配置文件后,需要重新加载,执行命令:

systemctl daemon-reload

启动service,命令:

systemctl start hello.service

或者:

systemctl start hello

查看service状态,命令:

systemctl status hello.service

或者:

systemctl status hello

设置service开机启动,命令:

systemctl enable hello

参考文献

Systemd 入门教程:命令篇

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值