Service

如何使用 systemd 對 Web 服務的正確關閉和啟動進行排序?

  • August 7, 2014

我正在嘗試使用 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

我的意圖是僅在 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

我指定的訂購週期似乎引起了問題。這應該如何解決?

據我所知,你有一個循環依賴。您告訴 systemd 在 Apache 之前和同時在 Apache 之後啟動 PHP-fpm。這不能按您希望的方式工作。

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

Requires=mariadb.service php-fpm.service
After=mariadb.service php-fpm.service

systemd 單元文件的選項說明。它還說關閉順序將是相反的啟動順序,因此您不必單獨配置。“Requires”部分將確保 Apache 僅在 MariaDB 和 PHP-fpm 成功啟動時啟動。

引用自:https://serverfault.com/questions/618612