Create Custom Unit and Start at Boot
You can create your custom unit file and run your designed processes at boot. On this topic page, we'll explain how Linux OS starts multiple processes at boot and how to create a custom unit file that can autostart at boot.
default.target and multi-user.target
By using the functionalities of systemd, you can build programs that auto-start at boot. As explained briefly in the previous sections, when Linux OS starts, systemd triggers default.target. In the default.target unit file, which is a symbolic link to another target, e.g., graphical.target, you can see that multi-user.target is set under the Requires directive. This means default.target triggers multi-user.target at boot.
Below is the unit file named graphical.target, which is set as default.target in this example.
[Unit]
Description=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes
By default, default.target and multi-user.target are stored under the /usr/lib/systemd/system directory.
.wants directory
A target or unit may have its .wants directory, which is used to trigger other units with want dependencies. Basically, the unit files stored in the .wants directory will be run when the unit owning the .wants directory is started.
Unit files under the .wants directories are generated when another unit file specifies the target or unit in the WantedBy directive, and the unit file is enabled by the systemctl enable
command. When the systemctl enable
command is executed, the unit file's symbolic link is created under the .wants directory whose target or unit is specified by the unit file. By default, targets or units may not have their .wants directories. The directories are created along with symbolic links.
Creating an auto start custom service unit with the multi-user.target.wants directory
The multi-user.target.wants directory is often used to trigger custom units at boot. When you want to create an autostart custom service unit, you can do it with the following two steps
- Step 1. Create a unit file for the service under the /etc/systemd/system directory. Make sure to include the following directives
- Under the [Service] section, include the commands you want to execute at boot (e.g., use ExecStart or other command trigger directives)
- Under the [Install] section, include
WantedBy=multi-user.target
- Step 2. Run the
systemctl enable
command to create a symbolic link of the custom unit file under the multi-user.target.wants directory. If you want to run the unit right away, you can use the--now
option in the command.
When you complete Step 2, the unit should be set to
Subscribe now for
uninterrupted access.