systemctl是Linux中管理systemd服务的核心工具,提供统一命令集来启动、停止、重启、查看服务状态及设置开机自启,支持并行启动、依赖管理与Cgroups资源控制,相比SysVinit更高效;通过创建/etc/systemd/system/下的.service文件可自定义服务,包含[Unit]、[Service]、[Install]三部分,常用命令如start、stop、status、enable、journalctl -u查看日志,故障排查需检查依赖、配置路径、权限、端口占用等问题。

systemctl
systemd
在使用 Linux 系统时,我们经常需要启动、停止、重启或查看各种后台服务。
systemctl
最常用的命令包括:
systemctl start [服务名]
sudo systemctl start nginx
systemctl stop [服务名]
sudo systemctl stop apache2
systemctl restart [服务名]
sudo systemctl restart sshd
systemctl reload [服务名]
restart
sudo systemctl reload nginx
systemctl status [服务名]
systemctl status mysql
systemctl enable [服务名]
sudo systemctl enable docker
systemctl disable [服务名]
sudo systemctl disable cups
systemctl list-units --type=service
systemctl list-units --type=service
systemctl --failed
systemctl --failed
journalctl -u [服务名]
sudo journalctl -u nginx

当我第一次接触
systemd
systemctl
SysVinit
Upstart
systemctl
systemd
SysVinit
/etc/init.d/
SysVinit
systemd
systemd
systemd
SysVinit
systemd
systemd
journalctl
SysVinit
/var/log
journalctl
systemd
.service
SysVinit
总的来说,
systemctl
systemd

有时候,我们需要运行一个自己编写的程序或脚本作为后台服务,并希望它能像系统自带的服务一样,通过
systemctl
systemd
自定义服务单元文件通常放在
/etc/systemd/system/
.service
my-app.service
.service
[Unit]
[Service]
[Install]
我们来看一个简单的例子,假设你有一个 Python 脚本
/opt/my-app/app.py
# /etc/systemd/system/my-app.service [Unit] Description=My Custom Python Application Service After=network.target # 定义服务启动的顺序,表示在网络服务启动后才启动 Requires=mysql.service # 如果你的应用依赖MySQL,可以加上这个 [Service] Type=simple # 最常见的类型,表示ExecStart是主进程 ExecStart=/usr/bin/python3 /opt/my-app/app.py # 服务的启动命令 WorkingDirectory=/opt/my-app/ # 设置工作目录 Restart=on-failure # 当服务意外退出时自动重启 User=myuser # 以哪个用户身份运行服务,提高安全性 Group=myuser # 以哪个用户组身份运行服务 [Install] WantedBy=multi-user.target # 定义服务在哪个目标下启用(开机自启动)
这里面有几个关键参数:
[Unit]
Description
After
Requires
After
Requires
[Service]
Type
simple
ExecStart
forking
oneshot
Type
systemd
ExecStart
ExecStop
systemd
ExecReload
WorkingDirectory
restart
on-failure
always
User
Group
[Install]
WantedBy
multi-user.target
graphical.target
创建完
.service
systemd
sudo systemctl daemon-reload
这一步是告诉
systemd
sudo systemctl enable my-app.service
sudo systemctl start my-app.service
systemctl status my-app.service
通过这些步骤,你的自定义应用就能像一个专业的系统服务一样被
systemctl
ExecStart

在实际运维中,服务出问题是家常便饭。
systemctl
1. 初步诊断:systemctl status
当一个服务表现异常或者无法启动时,我的第一反应总是
systemctl status [服务名]
systemctl status nginx
它会告诉你服务是否正在运行、最近的日志片段、进程ID、内存占用等信息。如果服务启动失败,这里通常会显示红色的错误信息,并给出最近几行的日志,这往往能提供初步的线索。
2. 深入挖掘:journalctl
如果
status
journalctl
journalctl
systemd
sudo journalctl -u [服务名]
这会显示该服务自启动以来的所有日志。
sudo journalctl -u [服务名] -f
-f
tail -f
sudo journalctl -u [服务名] --since "2 hours ago" --until "now" sudo journalctl -u [服务名] --since "2023-01-01 10:00:00"
当你需要回顾某个时间点发生的问题时,这个功能简直是救星。
sudo journalctl -u [服务名] -p err sudo journalctl -u [服务名] -p warning
-p
-p err
-x
sudo journalctl -xeu [服务名]
-x
3. 常见故障排查点:
systemctl list-dependencies [服务名]
.service
systemctl cat [服务名]
ExecStart
chmod +x
.service
LimitNOFILE
LimitNPROC
netstat -tulnp
ss -tulnp
我的经验是,很多时候一个服务启动不了,最后发现竟然是
ExecStart
systemctl status
journalctl
以上就是Linux怎么使用systemctl管理服务的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号