如何让一个进程成为 systemd 下的服务?

lip*_*ipo 12 linux systemd services daemon

命令是什么或者如何将进程变成 Linux 中的服务?服务本质上不就是一个守护进程吗?

Gre*_*ine 19

用户服务的示例是描述如何执行此操作的最简单方法。

假设您有一个名为 的二进制文件或脚本mytask,您希望将其作为服务运行,并且它位于 中/usr/local/bin/

在您的主目录 中创建一个systemd名为 的单元文件,其中包含以下内容:my_example.service~/.config/systemd/user/

[Unit]
Description=[My example task]

[Service]
Type=simple
StandardOutput=journal
ExecStart=/usr/local/bin/mytask

[Install]
WantedBy=default.target
Run Code Online (Sandbox Code Playgroud)

该行ExecStart是最相关的,因为您可以在该行中指定要运行的二进制文件或脚本的路径。

要使您的服务在启动时自动启动,请运行

systemctl --user enable my_example.service
Run Code Online (Sandbox Code Playgroud)

如果您想立即启动服务而不重新启动,请运行

systemctl --user start my_example.service
Run Code Online (Sandbox Code Playgroud)

如果你想停止服务,运行

systemctl --user stop my_example.service
Run Code Online (Sandbox Code Playgroud)

要检查服务的状态,请运行

systemctl --user status my_example.service
Run Code Online (Sandbox Code Playgroud)


Abd*_*lan 5

systemd术语中,服务是一种单元文件,还有服务、套接字、设备、安装、自动安装、交换、目标、路径、计时器、切片和范围。是的,它基本上是运行系统或用户守护程序的一种方式。你可以自己写。从上面的链接阅读官方文档,并且互联网上提供了许多教程。