1.7. Creating custom unit files
There are several use cases for creating unit files from scratch: you could run a custom daemon, create a second instance of some existing service as in Creating a custom unit file by using the second instance of the sshd service
On the other hand, if you intend just to modify or extend the behavior of an existing unit, use the instructions from Modifying existing unit files.
Procedure
-
To create a custom service, prepare the executable file with the service. The file can contain a custom-created script, or an executable delivered by a software provider. If required, prepare a PID file to hold a constant PID for the main process of the custom service. You can also include environment files to store shell variables for the service. Make sure the source script is executable (by executing the
chmod a+x) and is not interactive. Create a unit file in the
/etc/systemd/system/directory and make sure it has correct file permissions. Execute asroot:# touch /etc/systemd/system/<name>.service # chmod 664 /etc/systemd/system/<name>.serviceReplace <name> with a name of the service you want to create. Note that the file does not need to be executable.
Open the created
<name>.servicefile and add the service configuration options. You can use various options depending on the type of service you wish to create, see Unit file structure.The following is an example unit configuration for a network-related service:
[Unit] Description=<service_description> After=network.target [Service] ExecStart=<path_to_executable> Type=forking PIDFile=<path_to_pidfile> [Install] WantedBy=default.target-
<service_description> is an informative description that is displayed in journal log files and in the output of the
systemctl statuscommand. -
The
Aftersetting ensures that the service is started only after the network is running. Add a space-separated list of other relevant services or targets. - path_to_executable stands for the path to the actual service executable.
-
Type=forkingis used for daemons that make the fork system call. The main process of the service is created with the PID specified in path_to_pidfile. Find other startup types in Important [Service] section options. -
WantedBystates the target or targets that the service should be started under. Think of these targets as a replacement of the older concept of runlevels.
-
<service_description> is an informative description that is displayed in journal log files and in the output of the
Notify
systemdthat a new<name>.servicefile exists:# systemctl daemon-reload # systemctl start <name>.service警告Always execute the
systemctl daemon-reloadcommand after creating new unit files or modifying existing unit files. Otherwise, thesystemctl startorsystemctl enablecommands could fail due to a mismatch between states ofsystemdand actual service unit files on disk. Note, that on systems with a large number of units this can take a long time, as the state of each unit has to be serialized and subsequently deserialized during the reload.