1.14. Extending the default unit configuration
You can extend the default unit file with additional systemd configuration options.
Procedure
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.
Create a configuration file with the .conf suffix:
# touch /etc/systemd/system/name.service.d/<config_name>.confReplace <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=30Create small configuration files focused only on one task. Such files can be easily moved or linked to configuration directories of other services.
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.
Create a directory and a custom configuration file:
# mkdir /etc/systemd/system/httpd.service.d/# touch /etc/systemd/system/httpd.service.d/custom_script.confSpecify the script you want to execute after the main service process by inserting the following text to the
custom_script.conffile:[Service] ExecStartPost=/usr/local/bin/custom.shApply 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.