如何在Linux上创建Systemd服务

如何在Linux上创建Systemd服务如何创建Systemd服务。保存后执行:4、启动服务:5、查看服务状态:6、设置服务开启启动:7、停止服务:脚本参数说明。

大家好,欢迎来到IT知识分享网。如何在Linux上创建Systemd服务"

如何创建Systemd服务

1、Systemd服务 脚本一般存放在:/usr/lib/systemd 目录下, 目录下又有usersystem之分

/usr/lib/systemd/system # 系统服务,开机不需要登录就能运行的程序(相当于开机自启)
/usr/lib/systemd/user # 用户服务,需要登录后才能运行的程序

2、在/usr/lib/systemd/system或/usr/lib/systemd/user创建“your-service.service”文件并写入如下示例

[Unit]
Description=<description about this service>

[Service]
User=<user e.g. root>
WorkingDirectory=<directory_of_script e.g. /root>
ExecStart=<script which needs to be executed>
Restart=always

[Install]
WantedBy=multi-user.target

3、保存后执行:

sudo systemctl daemon-reload #重新加载服务

4、启动服务:

sudo systemctl start your-service.service

5、查看服务状态:

sudo systemctl status your-service.service

6、设置服务开启启动:

sudo systemctl enable your-service.service

7、停止服务:

sudo systemctl disable your-service.service

脚本参数说明

[Unit]模块参数

Description

简单的服务描述

Documentation

文档链接,多个文档用空格隔开,支持以下协议文档http://, https://, file:, info:, man:.

Requires

配置需要依赖的服务。如果本服务已经启动,那么依赖服务肯定也已经全部启动。如果依赖服务中有任何一个服务启动失败,那么systemd不会启动本服务。多个依赖服务用空格隔开。

Wants

Requires类似,但是这里设定的服务启动失败不会影响本服务启动。

BindsTo

Requires类似,但是如果依赖服务关闭,那么本服务也会停止。

PartOf

Requires类似,但是依赖服务停止和重启也同样会停止和重启本服务。

Conflicts

设定冲突服务,如果设定的服务已经启动,那么本服务将不会启动。多个服务用空格隔开。

Before, After

设定在本服务启动“前”、“后”需要启动的服务。多个服务用空格隔开。

OnFailure

设定如果本服务发生错误,需要启动的服务。多个服务用空格隔开。

[Service]模块参数

Type

字段定义启动类型。它可以设置的值如下。

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

RemainAfterExit

是否在服务所有进程都退出时还认为该服务在启动状态,默认为否。

GuessMainPID

A boolean value that specifies whether systemd should guess the main PID of a service if it cannot be determined reliably. This option is ignored unless Type=forking is set and PIDFile is not set. Defaults to yes.

PIDFile

设置一个绝对路径的文件来存储服务的PID。

An absolute filename pointing to the PID file of this daemon. Use of this option is recommended for services where Type=forking. Systemd reads the PID of the main process of the daemon after start-up of the service. Systemd does not write to the file configured here, although it removes the file after the service has shut down.

ExecStart

定义启动进程时执行的命令。

ExecStartPre, ExecStartPost

启动服务之前(之后)执行的命令。

ExecReload

重启服务时执行的命令。

ExecStop

停止服务时执行的命令。

ExecStopPost

停止服务之后执行的命令。

RestartSec

在重启服务之前等待时间。

TimeoutStartSec

等待服务启动的时间。

TimeoutStopSec

等待服务停止的时间。

TimeoutSec

该参数设置后同时设置TimeoutStartSec 和 TimeoutStopSec参数。

RuntimeMaxSec

服务最长启动时间。默认是无限制。

Restart

服务退出或者被kill掉后是否重新启动

  • no(默认值):退出后不会重启
  • on-success:只有正常退出时(退出状态码为0),才会重启
  • on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
  • on-abnormal:只有被信号终止和超时,才会重启
  • on-abort:只有在收到没有捕捉到的信号终止时,才会重启
  • on-watchdog:超时退出,才会重启
  • always:不管是什么退出原因,总是重启

WorkingDirectory

指定服务执行的根目录

[Install]模块参数

WantedBy

通常是定义哪些target能够运行服务一般是multi-user.target

参考:

https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

https://www.freedesktop.org/software/systemd/man/systemd.exec.html#WorkingDirectory=

https://docs.fedoraproject.org/en-US/quick-docs/understanding-and-administering-systemd/#_unit_parameters

所有参数设置参考:

https://www.freedesktop.org/software/systemd/man/systemd.directives.html

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/51104.html

(0)
上一篇 2024-04-27 10:15
下一篇 2024-04-27 17:26

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信