Create custom unit files

To modify properties of an existing unit or create a new unit from scratch, you need to create custom unit files.

Before you begin

  • 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.
  • To manage custom services, 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.
  • You have administrative privileges.

About this task

You could run a custom daemon or create a second instance of some existing service. For details, see creating a custom unit file by using the second instance of the sshd service.

However, to modify or extend the properties of an existing unit, use the instructions from Modifying existing unit files.

Procedure

  1. Create a unit file in the /etc/systemd/system/ directory:
    # touch /etc/systemd/system/<name>.service
  2. Apply the read-only permission to the unit file:
    # chmod 664 /etc/systemd/system/<name>.service

    Replace <name> with a name of the service you want to create.

  3. Open the created <name>.service file, and add the service configuration options. You can use various options depending on the type of service you need 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 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.
    • 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. Find other startup types in important [Service] section options.
    • WantedBy states the target or targets that the service should be started under. Think of these targets as a replacement of the older concept of runlevels.
  4. Notify systemd that a new <name>.service file exists:
    # systemctl daemon-reload
    # systemctl start <name>.service
    Important

    Always run the systemctl daemon-reload command after creating new unit files or modifying existing unit files. Otherwise, systemctl start or systemctl enable might fail due to mismatched systemd and service unit file states on disk. Systems with a large number of units can take a long time to reload. This is because the state of each unit is serialized and subsequently deserialized during the reload.