Creating Custom Unit Files
Creating Custom Unit Files
-
Prepare the executable file with the custom service. This can be 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. It is also possible to 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 as root:
touch /etc/systemd/system/name.service
chmod 664 /etc/systemd/system/name.serviceReplace name with a name of the service to be created. Note that file does not need to be executable.
-
Open the name.service file created in the previous step, and add the service configuration options. There is a variety of options that can be used depending on the type of service you wish to create, 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.targetWhere:
- service_description is an informative description that is displayed in journal log files and in the output of the systemctl status command.
- the After setting 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.
- WantedBy states the target or targets that the service should be started under. Think of these targets as of a replacement of the older concept of runlevels.
- Type=forking is 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.
-
Notify systemd that a new name.service file exists by executing the following command as root:
systemctl daemon-reload
systemctl start name.service
Always run the systemctl daemon-reload command after creating new unit files or modifying existing unit files. Otherwise, the systemctl start or systemctl enable commands could fail due to a mismatch between states
of systemd and actual service unit files on disk.
Example
systemctl edit --force --full servicename.service |

