Systemd: create a unit/service
Table of Contents
- [Unit]
- [Service]
- [Install]
- Start / Enable the service
- Stop / Disable the service
- User services
- More commands
- Timers
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
[Unit]
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.StartLimitIntervalSec: seconds/StartLimitBurst: number: A service can’t be started more than “number” times in a “seconds” interval.
[Service]
Type: start-up type. Default option issimple, 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.
[Install]
- This section is needed when you want to install your unit/service (
systemctl enable). WantedBy: is similar toAfterbut is interpreted only during installation of the unit. Most used option ismulti-user.target.
Start / Enable the service
- Once you’ve saved the file in
/etc/systemd/system/, you need to reload systemd withsudo 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, replacingmyservicewith 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.
User services
- You can create services for specific users. Service files can be inside
/etc/systemd/user/(with root permissions) or~/.config/systemd/user/. - Use
WantedBy=default.targetin the[Install]section. - When you manage these services, add
--userto thesystemctlcommands.
More commands
- 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 typesudo systemctl reload myservice.service.
Timers
- Check Systemd: Timers.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: