systemctl是管理systemd服务的核心工具,通过操作单元(unit)实现服务的启动、停止、重启、状态查看及开机自启等控制,支持依赖管理、并发启动、Cgroups资源隔离和统一日志,相比SysVinit更高效稳定,并可通过编写.service文件自定义服务,结合journalctl调试,实现精细化系统管理。

systemctl
systemd
SysVinit
Upstart
掌握
systemctl
systemd
systemctl
systemctl start <service_name>
systemctl start nginx
systemctl stop <service_name>
systemctl stop apache2
systemctl restart <service_name>
systemctl restart sshd
systemctl reload <service_name>
systemctl reload nginx
systemctl status <service_name>
systemctl status mysql
systemctl enable <service_name>
systemctl enable docker
systemctl disable <service_name>
systemctl disable firewalld
systemctl is-enabled <service_name>
systemctl is-enabled crond
systemctl mask <service_name>
/dev/null
systemctl mask cups
systemctl unmask <service_name>
systemctl unmask cups
systemctl list-units
systemctl list-unit-files
systemctl
systemd
从
SysVinit
systemd
systemd
/etc/init.d/
chkconfig
systemd
最显著的不同体现在初始化过程的并行化。
SysVinit
systemd
此外,
systemd
另一个我非常欣赏的特性是它统一的日志管理。通过
journalctl
/var/log
systemctl status
systemd
cron
所以,选择
systemd
systemd
有时候,系统自带的服务无法满足我们的需求,或者我们需要运行一个自定义的应用程序作为后台服务。这时,编写自己的
systemd
一个典型的
.service
[Unit]
[Service]
[Install]
/etc/systemd/system/
[Unit]
Description
systemctl status
After
After=network.target
Wants
Requires
Wants
Requires
[Service]
Type
simple
ExecStart
forking
ExecStart
oneshot
notify
systemd
ExecStart
ExecStop
ExecReload
WorkingDirectory
User
Group
Restart
on-failure
always
Environment
[Install]
enable
WantedBy
enable
WantedBy=multi-user.target
调试技巧: 编写完单元文件后,首先要做的不是直接启动,而是:
systemd
sudo systemctl daemon-reload
systemd
sudo systemd-analyze verify /etc/systemd/system/your-service.service
sudo systemctl start your-service && sudo systemctl status your-service
journalctl -u your-service.service -f
-f
ExecStart
ps aux | grep your-service
我个人的经验是,
ExecStart
systemctl
除了日常的启动、停止和状态查询,
systemctl
查看单元文件的实际内容:
systemctl cat <service_name>
systemd
/etc/systemd/system/<service_name>.d/
显示单元的所有属性:
systemctl show <service_name>
RestartSec
TimeoutStartSec
检查单元的活动状态:
systemctl is-active <service_name>
active
systemctl is-enabled <service_name>
enabled
systemctl is-failed <service_name>
failed
管理 systemd
systemd
SysVinit
multi-user.target
graphical.target
systemctl get-default
systemctl set-default multi-user.target
systemctl isolate graphical.target
使用 systemctl --user
systemd
systemctl --user start <user_service_name>
systemctl --user enable <user_service_name>
创建临时服务:
systemctl start --transient <service_name>
这些技巧能让你在日常管理和故障排除中更加游刃有余。深入理解
systemd
systemctl
以上就是Linux服务管理systemctl命令详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号