起因
有时候,Linux中的服务配置需要修改。Redhat系统管理教程的systemd一章里面有一个例子是介绍如何修改配置。选择的例子是httpd。例子是介绍在httpd的config文件中添加
Restart=always
RestartSec=5s
之后
systemctl reload httpd.service
重启服务,能够发现服务会等待5秒再启动,在前面这5秒里面如果用systemctl status httpd.service查看,会发现httpd的状态是reloading。
现象和解决思路
资料上介绍2行配置是放在[Unit]部分的,但是实验发现,如果放在[Unit]部分,会收到报错:
systemd[1]: /etc/systemd/system/httpd.service.d/override.conf:41: Unknown lvalue 'Restart' in section 'Unit'
网上搜索“Unknown lvalue 'Restart' in section 'Unit'”没有查到直接的解决方法,不过查到一篇文章提到:
“You are likely comparing systemd docs you've read online for a different version instead using the docs on your system that match your version.
Check man systemd.unit on your own system. You may find that on your version, the directive is named
StartLimitInterval= and should be used [Service], not [Unit].”
顺着这个思路,查了man systemd.unit,发现里面的确没有Restart和RestartSec选项。
然后查man systemd.service,里面说的很清楚:
A unit configuration file whose name ends in ".service" encodes information about a process controlled and supervised by systemd.
This man page lists the configuration options specific to this unit type. See systemd.unit(5) for the common options of all unit configuration files. The common configuration items are
configured in the generic "[Unit]" and "[Install]" sections. The service specific configuration options are configured in the "[Service]" section.
把2行配置放到[Service]部分之后,问题解决,systemctl reload httpd也的确观察到5秒钟的延迟
实际过程
修改的时候使用systemctl edit httpd命令,修改文件会被放在/etc/systemd/system/httpd.service.d/override.conf,这个文件的优先级会比/usr/lib/systemd/system/httpd.service.d/目录下的文件优先级高。
这个文件看上去是这个样子的:
cat /etc/systemd/system/httpd.service.d/override.conf
[Service]
Restart=always
RestartSec=5s
系统会自动把这里的配置整合到总的配置文件中去,然后在重载服务的时候会延迟5秒中会看到reloading状态。
systemct reload httpd &
要让这个命令到后台去做,否则这个终端会等待,无法输入下面的命令,而等它启动完毕,再看status的话,就又已经是active状态了。
systemctl status httpd
显示:
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/httpd.service.d
└─override.conf
/usr/lib/systemd/system/httpd.service.d
└─php-fpm.conf
Active: reloading (reload) since Tue 2023-01-24 21:54:59 CST; 16min ago
Docs: man:httpd.service(8)
Main PID: 20416 (httpd); Control PID: 21530 (httpd)
注意状态中的:
Active: reloading (reload) since Tue 2023-01-24 21:54:59 CST; 16min ago