Systemd: create a unit/service
In this tutorial you will learn to create services with systemd, so your scripts can be executed during system boot.
To do this, you need to create a
<service-name>.service file in
/etc/systemd/system/. This is a template of the file content:
[Unit] Description=service description [Service] ExecStart=command [Install] WantedBy=multi-user.target
Description: A brief description of the service.
Wants: Declare “weak” dependencies requirement.
Requires: Declare “strong” dependencies requirement.
After: Declare that service can be started only when other service/unit finish its startup. For example,
After: network.targetmeans service will start after network is configured.
Before: the inverse of After.
OnFailure: A space-separated list of one or more units that are activated when this unit enters the “failed” state.
OnSuccess: A space-separated list of one or more units that are activated when this unit enters the “inactive” state.
StartLimitBurst: number: A service can’t be started more than “number” times in a “seconds” interval.
Type: start-up type. Default option is
simple, more info about this in this link.
ExecStart: Command that is executed when this service is started.
Restart: indicates if service should be restarted when process exits cleanly (on-success), when process exits with a non-zero exit code (on-failure), should be restarted always (always) or never (no). Other values are on-abnormal, on-abort and on-watchdog.
RestartSec: Configures the time to sleep (in seconds) before restarting a service .
User: set the user that processes are executed as. Default is root.
- This section is needed when you want to install your unit/service (
WantedBy: is similar to
Afterbut is interpreted only during installation of the unit. Most used option is
Start / Enable the service
- Once you’ve saved the file in
/etc/systemd/system/, you need to reload systemd with
sudo systemctl daemon-reload(you don’t need “sudo” if you are a “root” user).
- Then, you can start your service with
sudo systemctl start myserice.service, replacing
myservicewith your service filename.
- If you want to install your service (service will start when computer boots up), type
sudo systemctl enable myservice.service.
- You can start and enable a service with one command:
sudo systemctl enable --now myservice.service.
Stop / Disable the service
- You can stop a service with
sudo systemctl stop myservice.service.
- To disable a service, so can’t be started when computer boots up, type
sudo systemctl disable myservice.service.
- When you uninstall some program, ensure that there are no orphaned units, and disable them if there are.
- You can create services for specific users. Service files can be inside
/etc/systemd/user/(with root permissions) or
- When you manage these services, add
- Restart a service with
sudo systemctl restart myservice.service.
- Check service status with
sudo systemctl status myservice.service.
- Some services allow to reload its configuration without needing a restart (like
httpd). In those cases, you can type
sudo systemctl reload myservice.service.
- Check Systemd: Timers.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles: