![为什么我的 systemd 单元无法在服务器重新启动时运行?](https://linux22.com/image/57220/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E7%9A%84%20systemd%20%E5%8D%95%E5%85%83%E6%97%A0%E6%B3%95%E5%9C%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%87%8D%E6%96%B0%E5%90%AF%E5%8A%A8%E6%97%B6%E8%BF%90%E8%A1%8C%EF%BC%9F.png)
当服务器重新启动时,我无法使 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 中的日志记录功能,以确定它找不到哪个文件或目录,但一无所获。
以前有其他人经历过这种行为吗?
答案1
所有服务在启动之前都需要加载和解析,并且当 systemd 解析所有服务时,您的服务在启动过程的早期不可用,因为它位于不同的分区上。
要解决这个问题,要么保留本地服务/etc/systemd/system/
,要么为您的发行版制作一个包并将它们放入/usr/lib/systemd/system/