Centos

為什麼我的 systemd 單元在伺服器重新啟動時無法執行?

  • May 13, 2016

當伺服器重新啟動時,我無法讓 systemd 服務單元文件正確執行。此伺服器是全新的 CentOS 7.0.1406 安裝。我正在嘗試使用多個 Apache httpd 實例來設置它。在較舊的 CentOS 安裝中,我的 httpd 實例執行良好,所以現在我正嘗試將這些配置移植到使用 systemd 的新 CentOS。

我可以啟用該服務,啟動它,重新啟動它並停止它,而不會出現任何問題。當我重新啟動時,它說“沒有這樣的文件或目錄”。如果我重新啟用該服務,一切都會再次執行,直到下一次重新啟動。

服務文件:

[Unit]
Description=Apache web server instance apache01
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/data/apacheinstances/apache01/logs/httpd.pid
ExecStart=/data/apacheinstances/apache01/bin/apachectl start
ExecStop=/data/apacheinstances/apache01/bin/apachectl stop
ExecReload=/data/apacheinstances/apache01/bin/apachectl reload
PrivateTmp=true
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

我是如何啟用它的:

$ sudo systemctl enable /data/apacheinstances/apache01/bin/apache01.service
ln -s '/data/apacheinstances/apache01/bin/apache01.service' '/etc/systemd/system/multi-user.target.wants/apache01.service'
ln -s '/data/apacheinstances/apache01/bin/apache01.service' '/etc/systemd/system/apache01.service'


$ sudo systemctl status apache01
apache01.service - Apache web server instance apache01
  Loaded: loaded (/data/apacheinstances/apache01/bin/apache01.service; enabled)
  Active: inactive (dead)

我是如何開始的:

$ sudo systemctl start apache01


$ sudo systemctl status apache01
apache01.service - Apache web server instance apache01
  Loaded: loaded (/data/apacheinstances/apache01/bin/apache01.service; enabled)
  Active: active (running) since Wed 2014-10-08 14:37:56 PDT; 13s ago
 Process: 1740 ExecStart=/data/apacheinstances/apache01/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1746 (httpd)
  CGroup: /system.slice/apache01.service
          +-1746 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1747 /usr/local/sbin/cronolog /data/apacheinstances/apache01/logs/error_log-%Y%m
          +-1748 /usr/local/sbin/cronolog /data/apacheinstances/apache01/logs/access_log-%Y%m
          +-1749 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1750 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1751 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start

Oct 08 14:37:56 server105 apachectl[1740]: Starting apache01: [  OK  ]
Oct 08 14:37:56 server105 systemd[1]: PID file /data/apacheinstances/apache01/logs/httpd.pid not readable (yet?) after start.
Oct 08 14:37:56 server105 systemd[1]: Started Apache web server instance apache01.

我如何重新啟動它:

$ sudo systemctl restart apache01


$ sudo systemctl status apache01
apache01.service - Apache web server instance apache01
  Loaded: loaded (/data/apacheinstances/apache01/bin/apache01.service; enabled)
  Active: active (running) since Wed 2014-10-08 14:38:40 PDT; 12s ago
 Process: 1836 ExecStop=/data/apacheinstances/apache01/bin/apachectl stop (code=exited, status=0/SUCCESS)
 Process: 1844 ExecStart=/data/apacheinstances/apache01/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1850 (httpd)
  CGroup: /system.slice/apache01.service
          +-1850 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1851 /usr/local/sbin/cronolog /data/apacheinstances/apache01/logs/error_log-%Y%m
          +-1852 /usr/local/sbin/cronolog /data/apacheinstances/apache01/logs/access_log-%Y%m
          +-1853 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1855 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1856 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start

Oct 08 14:38:40 server105 apachectl[1844]: Starting apache01: [  OK  ]
Oct 08 14:38:40 server105 systemd[1]: PID file /data/apacheinstances/apache01/logs/httpd.pid not readable (yet?) after start.
Oct 08 14:38:40 server105 systemd[1]: Started Apache web server instance apache01.

我是如何阻止它的:

$ sudo systemctl stop apache01


$ sudo systemctl status apache01
apache01.service - Apache web server instance apache01
  Loaded: loaded (/data/apacheinstances/apache01/bin/apache01.service; enabled)
  Active: inactive (dead) since Wed 2014-10-08 14:39:44 PDT; 12s ago
 Process: 1940 ExecStop=/data/apacheinstances/apache01/bin/apachectl stop (code=exited, status=0/SUCCESS)
 Process: 1844 ExecStart=/data/apacheinstances/apache01/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1850 (code=exited, status=0/SUCCESS)

Oct 08 14:38:40 server105 apachectl[1844]: Starting apache01: [  OK  ]
Oct 08 14:38:40 server105 systemd[1]: PID file /data/apacheinstances/apache01/logs/httpd.pid not readable (yet?) after start.
Oct 08 14:38:40 server105 systemd[1]: Started Apache web server instance apache01.
Oct 08 14:39:44 server105 systemd[1]: Stopping Apache web server instance apache01...
Oct 08 14:39:44 server105 apachectl[1940]: Stopping apache01: [  OK  ]
Oct 08 14:39:44 server105 systemd[1]: Stopped Apache web server instance apache01.

重啟後狀態:

$ sudo systemctl status apache01
apache01.service
  Loaded: not-found (Reason: No such file or directory)
  Active: inactive (dead)

Oct 08 14:44:58 server105 systemd[1]: Cannot add dependency job for unit apache01.service, ignoring: Unit apache01.service failed to load: No such file or directory.

我如何重新啟用並啟動它:

$ sudo systemctl reenable /data/apacheinstances/apache01/bin/apache01.service
rm '/etc/systemd/system/apache01.service'
rm '/etc/systemd/system/multi-user.target.wants/apache01.service'
ln -s '/data/apacheinstances/apache01/bin/apache01.service' '/etc/systemd/system/multi-user.target.wants/apache01.service'
ln -s '/data/apacheinstances/apache01/bin/apache01.service' '/etc/systemd/system/apache01.service'


$ sudo systemctl start apache01


$ sudo systemctl status apache01
apache01.service - Apache web server instance apache01
  Loaded: loaded (/data/apacheinstances/apache01/bin/apache01.service; enabled)
  Active: active (running) since Wed 2014-10-08 14:52:20 PDT; 5s ago
 Process: 1737 ExecStart=/data/apacheinstances/apache01/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 1743 (httpd)
  CGroup: /system.slice/apache01.service
          +-1743 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1744 /usr/local/sbin/cronolog /data/apacheinstances/apache01/logs/error_log-%Y%m
          +-1745 /usr/local/sbin/cronolog /data/apacheinstances/apache01/logs/access_log-%Y%m
          +-1746 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1747 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start
          +-1748 /usr/local/httpd-2.4.10-apache01/bin/httpd -f /data/apacheinstances/apache01/conf/httpd.conf -k start

Oct 08 14:52:20 server105 apachectl[1737]: Starting apache01: [  OK  ]
Oct 08 14:52:20 server105 systemd[1]: PID file /data/apacheinstances/apache01/logs/httpd.pid not readable (yet?) after start.
Oct 08 14:52:20 server105 systemd[1]: Started Apache web server instance apache01.

我不確定為什麼它在重新啟動之前沒有問題但之後卻失敗了。如果我不重新啟用,我會繼續收到關於“沒有這樣的文件或目錄”的相同錯誤。

我試圖研究 systemd 中的日誌記錄功能,以確定它無法找到哪個文件或目錄,但已經畫了一個空白。

以前有沒有其他人經歷過這種行為?

所有服務都需要在啟動之前載入和解析,並且當 systemd 解析所有服務時,您的服務在啟動過程的早期不可用,因為它位於不同的分區上。

要解決它,要麼保留本地服務,/etc/systemd/system/要麼為您的分發製作一個包並將它們放入/usr/lib/systemd/system/

引用自:https://unix.stackexchange.com/questions/160126