systemctl是Linux下systemd系统的服务管理命令,统一管理服务生命周期。通过start、stop、restart、reload控制服务启停,enable/disable设置开机自启,status查看状态,配合journalctl排查日志。systemd优势在于统一接口、并行启动、按需激活、依赖管理、cgroups资源隔离和集中日志。创建自定义服务需编写.service文件,包含[Unit]、[Service]、[Install]三部分,设置描述、启动命令、用户权限、重启策略等,并执行daemon-reload加载配置。排查问题用status和journalctl -xeu查看详细日志,检查路径、权限、环境变量和Type类型匹配。Target替代传统运行级别,如multi-user.target、graphical.target,通过get-default查看默认目标,isolate切换目标,set-default设置默认。list-units和list-unit-files列出所有单元及其状态,show查看单元属性,list-dependencies展示依赖关系。masked状态可彻底禁用服务,防止启动。

说起Linux下的服务管理,现在的主流选择无疑是
systemctl
对于一个Linux系统管理员或者开发者来说,掌握
systemctl
service
/etc/init.d/
核心操作示例:
systemctl start <service_name>
systemctl start nginx
systemctl stop <service_name>
systemctl stop nginx
systemctl restart <service_name>
systemctl restart nginx
systemctl reload <service_name>
systemctl reload nginx
systemctl status <service_name>
systemctl status nginx
systemctl enable <service_name>
systemctl enable nginx
/etc/systemd/system/multi-user.target.wants/
.service
systemctl disable <service_name>
systemctl disable nginx
systemctl is-enabled <service_name>
enabled
disabled
systemctl list-units --type=service
systemctl list-unit-files --type=service
有时候,你会发现服务启动失败,
systemctl status
journalctl -xeu <service_name>
我记得刚接触Linux那会儿,不同发行版的服务启动脚本写法都不太一样,排查问题简直是噩梦。Systemd出现后,虽然初期有些争议,但它带来的统一性、并行启动的效率提升,以及更完善的依赖管理,确实让系统管理变得省心多了。尤其是开机速度,那真是肉眼可见的快。
Systemd作为Linux系统的初始化系统和服务管理器,与传统的SysVinit或Upstart相比,带来了以下显著优势:
systemctl
Requires
Wants
Before
After
journald
journalctl
.service
我曾经遇到过一个服务,在命令行下运行得好好的,一写成
.service
ExecStart
[Service]
journalctl
创建自定义Systemd服务
创建一个自定义服务通常涉及到编写一个
.service
编写.service
/etc/systemd/system/
my_app.service
# /etc/systemd/system/my_app.service [Unit] Description=My Custom Python Application After=network.target # 定义服务在哪个目标(Target)之后启动 [Service] ExecStart=/usr/bin/python3 /opt/my_app/app.py # 启动服务的命令 WorkingDirectory=/opt/my_app # 服务的工作目录 Restart=always # 定义服务退出后如何重启 (no, on-success, on-failure, on-abnormal, on-watchdog, on-abort, always) User=myuser # 以哪个用户身份运行服务 Group=myuser # 以哪个用户组身份运行服务 StandardOutput=journal # 将标准输出重定向到journald StandardError=journal # 将标准错误重定向到journald [Install] WantedBy=multi-user.target # 定义服务被哪个目标(Target)“需要”,从而实现开机自启
[Unit]
After=network.target
[Service]
ExecStart
WorkingDirectory
Restart
User
Type
simple
forking
oneshot
notify
[Install]
WantedBy=multi-user.target
multi-user.target
重新加载Systemd配置:
sudo systemctl daemon-reload
这会通知Systemd重新扫描所有单元文件,加载新的或修改过的配置。
启动服务并设置开机自启:
sudo systemctl start my_app.service sudo systemctl enable my_app.service
排查Systemctl服务启动问题
当服务无法按预期启动时,以下是一些常用的排查步骤:
查看服务状态:
systemctl status my_app.service
这是最直接的诊断工具,它会显示服务当前状态、最近的日志片段、PID、CGroup信息等。留意
Active:
active (running)
failed
exited
检查详细日志:
journalctl -xeu my_app.service
这是排查问题的杀手锏。
-x
-e
-u
验证ExecStart
ExecStart
+x
ExecStart
检查文件和目录权限: 确保服务运行的用户(在
User=
WorkingDirectory
检查依赖关系: 如果服务依赖于其他服务或资源(如网络、数据库),确保这些依赖项已经正确启动。
systemctl list-dependencies my_app.service
环境变量问题: 有时候服务在命令行下能跑,但通过
systemctl
.service
[Service]
Environment=
EnvironmentFile=
Type
forking
Type=simple
Type
刚开始接触Systemd的Target时,我有点懵,感觉就是把运行级别换了个名字。但深入了解后,发现它比运行级别更灵活,可以自定义,也可以动态切换。特别是
mask
Systemd Targets(目标)
Systemd中的“目标”(Target)是用来替代传统SysVinit运行级别(runlevel)的概念。它不是一个单一的服务,而是一组要启动的服务或一个特定的系统状态。每个Target都代表了系统可能达到的一种状态或模式。
.target
Wants=
Requires=
multi-user.target
graphical.target
multi-user.target
reboot.target
poweroff.target
rescue.target
systemctl get-default
这会显示系统启动时默认进入的Target。
systemctl isolate <target_name>
例如,
systemctl isolate multi-user.target
isolate
sudo systemctl set-default <target_name>
例如,
sudo systemctl set-default graphical.target
列出和管理Systemd单元
除了服务(
.service
.target
.device
.mount
.socket
.timer
列出所有已加载的单元:
systemctl list-units
这会显示所有当前Systemd进程已知的单元,包括活动的、不活动的、加载的、未加载的等。
列出所有单元文件的状态:
systemctl list-unit-files
这个命令会显示所有可用的单元文件及其是否被
enabled
disabled
static
masked
查看特定单元的详细属性:
systemctl show <unit_name>
例如,
systemctl show nginx.service
查看单元的依赖关系树:
systemctl list-dependencies <unit_name>
这个命令可以帮助你理解一个服务或目标所依赖的其他单元,以及哪些单元依赖于它
以上就是如何在Linux中管理服务 Linux systemctl启停服务的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号