如何使用 systemd 对 Web 服务的正确关闭和启动进行排序?

Que*_*low 3 startup service systemd shutdown

我正在尝试使用 systemd 来构建 Apache、PHP-FPM 和 MariaDB 服务的关闭和启动:

这些是文件/etc/systemd/system夹中的其他配置文件:

# httpd.service
.include /usr/lib/systemd/system/httpd.service
[Unit]
After=mariadb.service php-fpm.service
Before=php-fpm.service

# php-fpm.service
.include /usr/lib/systemd/system/php-fpm.service
[Unit]
Before=mariadb.service
Run Code Online (Sandbox Code Playgroud)

我的意图是仅在 PHP-FPM 和 MariaDB 启动后才启动 Apache,并在停止 PHP-FPM 之前停止 Apache,在 MariaDB 之前停止 PHP-FPM。

但是,我在启动和关闭时都遇到错误:

12:42:09 systemd[1]: Found ordering cycle on php-fpm.service/stop
12:42:09 systemd[1]: Found dependency on mariadb.service/stop
12:42:09 systemd[1]: Found dependency on php-fpm.service/stop
12:42:09 systemd[1]: Job httpd.service/stop deleted to break ordering cycle starting with php-fpm.service/stop
12:42:09 systemd[1]: Stopping MariaDB database server...
12:42:12 systemd[1]: Stopped MariaDB database server.
12:42:12 systemd[1]: Stopping The PHP FastCGI Process Manager...
12:42:12 systemd[1]: Failed to remove content of temporary directory /tmp/systemd-mariadb.service-Xp7JJZ5: No such file or directory
12:42:12 systemd[1]: Stopped The PHP FastCGI Process Manager.
12:42:12 systemd[1]: Failed to remove content of temporary directory /tmp/systemd-php-fpm.service-XPLabUE: No such file or directory
-- Reboot --
12:46:20 systemd[1]: Found ordering cycle on php-fpm.service/start
12:46:20 systemd[1]: Found dependency on mariadb.service/start
12:46:20 systemd[1]: Found dependency on php-fpm.service/start
12:46:20 systemd[1]: Job httpd.service/start deleted to break ordering cycle starting with php-fpm.service/start
Run Code Online (Sandbox Code Playgroud)

我指定的订购周期似乎引起了问题。这应该如何解决?

小智 6

据我所知,你有一个循环依赖。您告诉 systemd 在 Apache 之前和 Apache 之后启动 PHP-fpm。这不能按照您希望的方式工作。

在您的httpd.service文件中指定以下内容:

Requires=mariadb.service php-fpm.service
After=mariadb.service php-fpm.service
Run Code Online (Sandbox Code Playgroud)

systemd 单元文件的选项说明。它还说关闭顺序将是相反的启动顺序,因此您不必单独配置。“Requires”部分将确保 Apache 仅在 MariaDB 和 PHP-fpm 成功启动时启动。