1.14. Extending the default unit configuration


You can extend the default unit file with additional systemd configuration options.

Procedure

  1. Create a configuration directory in /etc/systemd/system/:

    # mkdir /etc/systemd/system/<name>.service.d/

    Replace <name> with the name of the service you want to extend. The syntax applies to all unit types.

  2. Create a configuration file with the .conf suffix:

    # touch /etc/systemd/system/name.service.d/<config_name>.conf

    Replace <config_name> with the name of the configuration file. This file adheres to the normal unit file structure and you have to specify all directives in the appropriate sections, see Unit file structure.

    For example, to add a custom dependency, create a configuration file with the following content:

    [Unit]
    Requires=<new_dependency>
    After=<new_dependency>

    The <new_dependency> stands for the unit to be marked as a dependency. Another example is a configuration file that restarts the service after its main process exited, with a delay of 30 seconds:

    [Service]
    Restart=always
    RestartSec=30

    Create small configuration files focused only on one task. Such files can be easily moved or linked to configuration directories of other services.

  3. Apply changes to the unit:

    # systemctl daemon-reload
    # systemctl restart <name>.service

例 1.1. Extending the httpd.service configuration

To modify the httpd.service unit so that a custom shell script is automatically executed when starting the Apache service, perform the following steps.

  1. Create a directory and a custom configuration file:

    # mkdir /etc/systemd/system/httpd.service.d/
    # touch /etc/systemd/system/httpd.service.d/custom_script.conf
  2. Specify the script you want to execute after the main service process by inserting the following text to the custom_script.conf file:

    [Service]
    ExecStartPost=/usr/local/bin/custom.sh
  3. Apply the unit changes::

    # systemctl daemon-reload
    # systemctl restart httpd.service
注意

The configuration files from the /etc/systemd/system/ configuration directories take precedence over unit files in /usr/lib/systemd/system/. Therefore, if the configuration files contain an option that can be specified only once, such as Description or ExecStart, the default value of this option is overridden. Note that in the output of the systemd-delta command, described in Monitoring overridden units ,such units are always marked as [EXTENDED], even though in sum, certain options are actually overridden.

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部