“systemctl start”和“systemctl enable”有什么区别?

“systemctl start”和“systemctl enable”有什么区别?

我在计算机上安装了 MariaDB 服务器。在设置过程中,我遇到了是否应始终启用它的问题,我遵循的文档建议执行以下步骤:

sudo yum install mariadb mariadb-server 
sudo systemctl start mariadb.service  
sudo systemctl enable mariadb.service

答案1

systemctl startsystemctl enable做不同的事情。

enable将指定的单元挂接到相关位置,以便它在启动时自动启动,或者在插入相关硬件时自动启动,或者根据单元文件中指定的内容在其他情况下自动启动。

start立即启动该装置。

disablestop分别与它们相反。

这意味着,当您首次安装 MariaDB 时,您可能需要运行systemctl enable mariadb.service来启用它,以便它在启动时启动。您可能还想运行systemctl start mariadb.service,或者只是重新启动,以便启动 MariaDB。要停止 MariaDB,请运行systemctl stop mariadb.service(它将在下次启动时或您手动启动时再次启动)。要禁用它,以便它不再在启动时启动,请运行systemctl disable mariadb.service

更新:

正如 gerardw 的回答中所述,从版本 220,于 2015 年 5 月发布,enabledisable开始采用可选--now开关,以便根据所使用的命令启动或停止设备。

要使用同一命令禁用和停止一个单元,请使用systemctl disable mariadb.service --now。同样,要启用和启动一个单元,请使用systemctl enable mariadb.service --now

来源:systemctl 手册页

答案2

手册systemctl

enable NAME...
   Enable one or more unit files or unit file instances, as specified
   on the command line. This will create a number of symlinks as
   encoded in the "[Install]" sections of the unit files. After the
   symlinks have been created, the systemd configuration is reloaded
   (in a way that is equivalent to daemon-reload) to ensure the
   changes are taken into account immediately. Note that this does not
   have the effect of also starting any of the units being enabled. If
   this is desired, either --now should be used together with this
   command, or an additional start command must be invoked for the
   unit.
   ...
   Enabling units should not be confused with starting (activating)
   units, as done by the start command. Enabling and starting units is
   orthogonal: units may be enabled without being started and started
   without being enabled. Enabling simply hooks the unit into various
   suggested places (for example, so that the unit is automatically
   started on boot or when a particular kind of hardware is plugged
   in). Starting actually spawns the daemon process (in case of
   service units), or binds the socket (in case of socket units), and
   so on.

本质上,enable将服务标记为在启动时启动,并start实际上立即启动该服务。

答案3

从 systemctl 版本 220 开始,使能够禁用支持- 现在切换到与启用/禁用同时启动/停止服务。

例如systemctl --now enable foobar.service

用于systemctl --version检查您安装的版本。

相关内容