Systemd: Service File Examples

本文介绍如何使用systemd创建服务文件,重启及重载unit文件,启用服务,并详细解释了systemd服务文件的重要选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

$ man systemd.unit
Important Section Options[Install]
OptionDescription
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]
OptionDescription
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

转载于:https://www.cnblogs.com/gardenofhu/p/11497659.html

This error message indicates that the systemd service unit file for the Zabbix server service could not be located or is missing. To fix this issue, you can try the following steps: 1. Check if the Zabbix server package is installed on your system using the command: `dpkg -l zabbix-server*` or `rpm -qa | grep zabbix-server`. 2. If the package is not installed, install it using the appropriate package manager command for your system, such as `sudo apt install zabbix-server-mysql` for Debian/Ubuntu or `sudo yum install zabbix-server-mysql` for CentOS/RHEL. 3. If the package is already installed, try reinstalling it using `sudo apt-get --reinstall install zabbix-server-mysql` for Debian/Ubuntu or `sudo yum reinstall zabbix-server-mysql` for CentOS/RHEL. 4. If the issue persists, check if the systemd service file for Zabbix server exists in the `/etc/systemd/system/` directory. If it is missing, you can try to create it manually by copying the sample service file from the Zabbix server package installation directory. For example, on Debian/Ubuntu systems, you can use the following command: `sudo cp /usr/share/doc/zabbix-server-mysql/examples/systemd/zabbix-server.service /etc/systemd/system/`. 5. Finally, reload the systemd daemon and start the Zabbix server service using the following commands: ``` sudo systemctl daemon-reload sudo systemctl start zabbix-server.service ``` If the above steps do not resolve the issue, you may need to check the system logs for more detailed error messages or seek assistance from the Zabbix community.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值