Linux 中的 Systemctl 命令:重启、重新加载和停止服务

热门:
升级您的服务器配置! 申请 AVA 并使用 立减 15%
使用优惠码:

Systemctl 命令:在 Linux 中重启、重新加载和停止服务

在使用 systemd 作为 init system 的现代 Linux distributions 中,管理服务通常使用 systemctl command。无论你是维护 web server 的 administrator,还是测试 application changes 的 developer,了解如何重启、重新加载和停止服务都至关重要。

本文通过实际示例拆解最常见的 systemctl commands。

 什么是 systemctl?

systemctl 是用于控制 systemd system and service manager 的 command-line utility。它允许你在系统上启动、停止、重启、重新加载、启用、禁用和监控 services。

1. 重启服务

当你想要 完全停止然后再次启动 一个 service 时使用此命令。在修改 configuration changes 后,或当 service 变得无响应时,它很有用。

语法:

sudo systemctl restart <service-name>

示例:

sudo systemctl restart nginx
这会停止然后启动 NGINX web server,并应用更新后的 settings。

 2. 重新加载服务

reload 会告诉 service 在不重启整个 process 的情况下重新加载其 configuration。并非所有 services 都支持此功能。

语法:

sudo systemctl reload <service-name>

示例:

sudo systemctl reload apache2
Apache 将在不影响 active connections 的情况下重新加载其 configuration。

提示:

你可以检查某个 service 是否支持 reload:

systemctl show <service-name> | grep CanReload

3. 停止服务

使用此命令来 终止正在运行的 service。它将保持 inactive,直到你手动再次启动它或重启系统(除非它已在 boot 时启用)。

语法:

sudo systemctl stop <service-name>

示例:

sudo systemctl stop mysql
这会停止 MySQL database server。

结论

掌握像 systemctl 这样的命令,以及 restartreloadstop,是高效管理 Linux 中 services 的关键。它们让你能够应用 updates、修复 issues,并精确控制 system behavior。

始终记得使用以下命令验证更改:

systemctl status <service-name>