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


83

我在计算机上安装了MariaDB服务器。设置时遇到了一个问题,我是否必须一直启用它,因为我遵循的文档包含以下步骤,

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

2
您也可以enable start--now的SWICH enable子命令,例如:systemctl enable sshd --now。对于服务的disable命令也是如此stop

Answers:


131

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

资料来源:systemctl手册页


4
++++清楚的解释
sunleo

那么什么时候应该键入systemctl enable?每当我们要运行systemd服务时?
Goldname

不,要使其自动启动时。如果要停止自动启动,则运行systemctl disable
vurp0

就是说,服务是从root启动的吗?如何在启动时以其他用户身份启动服务?
Chaminda Bandara

18

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实际上立即启动该服务。


我们什么时候启用?我们只需要做一次吗?
Goldname

@Goldname您需要在下次启动时首次启动时启用它。仅一次就足够了
大师

那我们编辑的时候呢?我们需要重新启用吗?
Goldname '17

@Goldname,您无需重新启用它。
muru

谢谢,如果将文件移动到其他目录和/或重新启动系统,情况也会一样吗?
金牌名称'17

4

从systemctl版本220开始,启用禁用支持--now开关,以在启用/禁用的同时启动/停止服务。

例如 systemctl --now enable foobar.service

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.