Systemctl和服务命令的区别

Difference between Systemctl and service command

我总是这样 运行 我的 Apache 服务器:

sudo service apache2 start

但是在很多指南中我看到这样的命令:

sudo systemctl start apache2

谁能简单写下这些命令之间的区别,并给我建议我在日常工作中应该使用什么命令?

service/etc/init.d 中的文件进行操作,并与旧的 init 系统结合使用。 systemctl/lib/systemd 中的文件进行操作。如果 /lib/systemd 中有您的服务文件,它将首先使用该文件,如果没有,它将回退到 /etc/init 中的文件.d。 此外,如果您使用 OS,例如 ubuntu-14.04,则只有服务命令可用。

所以如果systemctl可用的话,用它会更好

借自this answer

service is an "high-level" command used for start, restart, stop and status services in different Unixes and Linuxes. Depending on the "lower-level" service manager, service redirects on different binaries.

For example, on CentOS 7 it redirects to systemctl, while on CentOS 6 it directly called the relative /etc/init.d script. On the other hand, in older Ubuntu releases it redirects to upstart.

service is adequate for basic service management, while directly calling systemctl give greater control options.

此外,systemctl 从此路径运行服务文件:/lib/systemd/

服务作为 sysvinit 的帮助脚本启动,后者成为包装脚本,无论使用 sysvinit 还是 systemd 都可以正常工作。

我的选择service更可靠,尤其是在自动化任务中。它包装了不同初始化系统的命令,因此您可以将它用于较旧的发行版并期望得到相同的结果。根据我的观察,当 systemd 单元正常与 service <service-name> reload 一起工作时,通常没有工作 reload 命令(更新守护进程配置而不杀死进程)。