答案:创建自定义systemd服务需编写.service文件并放置于/etc/systemd/system/,通过systemctl管理。具体步骤包括:使用绝对路径在[Service]中定义ExecStart,设置User、WorkingDirectory等参数,选择合适的Type类型(如simple、forking),配置Restart=on-failure实现故障重启;创建后运行sudo systemctl daemon-reload、enable、start启用服务,并用status和journalctl排查启动失败问题,常见原因有路径权限错误、脚本异常、Type类型不匹配等。

在Linux中创建自定义的systemd服务,核心在于编写一个
.service
systemctl
systemd
要让你的应用或脚本在Linux上像个“正经”的服务一样运行,被
systemd
.service
systemd
首先,我们得创建一个
.service
/etc/systemd/system/
my_custom_app.service
sudo vim /etc/systemd/system/my_custom_app.service
文件内容大致会是这样:
[Unit] Description=我的自定义应用程序服务 After=network.target # 这个服务在网络可用后启动 [Service] Type=simple # 简单类型,表示ExecStart命令就是主进程,它会一直运行在前景 ExecStart=/usr/local/bin/my_custom_app_script.sh # 你的应用程序或脚本的完整路径 WorkingDirectory=/opt/my_custom_app/ # 设置工作目录,如果你的脚本需要 User=your_username # 指定运行服务的用户,建议不要用root,除非必要 Group=your_group # 指定运行服务的用户组 Restart=on-failure # 当服务失败时(非正常退出),自动重启 RestartSec=5s # 重启前等待5秒 [Install] WantedBy=multi-user.target # 在多用户模式下启用此服务(即系统启动时)
这里面有几个关键部分:
[Unit]
Description
After=network.target
After=mysql.service
[Service]
Type
systemd
ExecStart
simple
Type=forking
Type
ExecStart
systemd
WorkingDirectory
User
Group
root
Restart
on-failure
systemd
RestartSec
Restart
[Install]
WantedBy=multi-user.target
文件创建并保存后,你需要让
systemd
sudo systemctl daemon-reload
接着,启用你的服务,让它在系统启动时自动运行:
sudo systemctl enable my_custom_app.service
然后,你可以手动启动它:
sudo systemctl start my_custom_app.service
最后,检查服务状态,看看它是否正常运行:
sudo systemctl status my_custom_app.service
如果一切顺利,你会看到服务处于
active (running)
status
journalctl -u my_custom_app.service
这就是创建和管理一个基本
systemd

服务启动失败,这简直是家常便饭。我遇到过太多次了,通常不是
systemd
最常见的几个原因,我总结了一下:
ExecStart
systemd
PATH
ExecStart=/path/to/your_script.sh
python app.py
/usr/bin/python /opt/my_app/app.py
chmod +x /path/to/your_script.sh
systemd
User=your_username
./config.json
WorkingDirectory
After=network.target
Requires=mysql.service
Type
Type=simple
systemd
Type=forking
systemd
排查步骤,我一般是这么来:
sudo systemctl status my_custom_app.service
这个命令会给你一个快速概览,包括服务的状态、最近的错误信息,以及一些日志片段。 如果需要更详细的日志,用
journalctl
sudo journalctl -u my_custom_app.service --since "10 minutes ago" -e
-u
--since
-e
ExecStart
ExecStart
# 假设你的服务用户是your_username,工作目录是/opt/my_custom_app/ sudo -u your_username sh -c "cd /opt/my_custom_app/ && /usr/local/bin/my_custom_app_script.sh"
这样可以直接看到脚本的输出和错误信息,模拟
systemd
echo "Hello World"
ExecStart
systemd
systemd
[Service]
Environment=KEY=VALUE
EnvironmentFile=/path/to/env_file
记住,日志是你的好朋友。大部分问题,日志里都会给出线索。

Type
systemd
systemd
Type
systemd
我们来看看几个最常见的
Type
Type=simple
ExecStart
systemd
ExecStart
systemd
simple
Type=forking
ExecStart
fork
systemd
systemd
PIDFile=/path/to/pidfile.pid
systemd
forking
PIDFile
Type=oneshot
ExecStart
systemd
oneshot
RemainAfterExit=yes
ExecStart
Type=notify
simple
sd_notify()
systemd
systemd
systemd
notify
libsystemd
如何选择?
我的经验是:
Type=simple
Type=forking
PIDFile
Type=oneshot
Type=notify
如果不确定,先从
simple
forking
notify
systemd

让
systemd
systemd
自动重启策略 (Restart=
这是让服务在出现问题时自动恢复的关键。我通常会根据服务的性质来选择合适的重启策略。
Restart=no
Type=oneshot
Restart=on-success
Restart=on-failure
SIGSEGV
systemd
systemd
Restart=on-abnormal
Restart=on-watchdog
systemd
Restart=always
systemd
通常,我会在
[Service]
Restart=on-failure RestartSec=5s # 重启前等待5秒,避免服务在极短时间内反复崩溃又重启
RestartSec
为了防止无限重启导致的问题,
systemd
StartLimitIntervalSec=60s
以上就是Linux怎么创建自定义的systemd服务的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号