In this tutorial, we will learn how to create and manage a systemd service on a Linux system. Systemd is a system and service manager for Linux operating systems that provides a central management point for services and processes.
The first step in creating a systemd service is to create a service file. This file will define the properties of the service, such as its name, description, and startup parameters.
To create the service file, open a text editor and create a new file with the .service extension. For example:
sudo nano /etc/systemd/system/my-service.service
Next, we need to define the service in the service file. This includes specifying the service name, description, and startup parameters.
Here is an example of what a basic service file might look like:
[Unit]
Description=My Service
[Service]
ExecStart=/usr/bin/my-service
Restart=always
[Install]
WantedBy=multi-user.target
After defining the service, we need to configure it to run as a systemd service. To do this, we need to reload the systemd configuration and enable the service.
sudo systemctl daemon-reload
sudo systemctl enable my-service
To start the service, use the following command:
sudo systemctl start my-service
To stop the service, use the following command:
sudo systemctl stop my-service
To restart the service, use the following command:
sudo systemctl restart my-service
To enable the service to start automatically at boot, use the following command:
sudo systemctl enable my-service
To disable the service from starting automatically at boot, use the following command:
sudo systemctl disable my-service
To check the status of the service, use the following command:
sudo systemctl status my-service
This will show you whether the service is running, stopped, or failed.
[Unit]: This section contains metadata and dependencies for the service.
Description: A short description of the service.
[Service]: This section contains information about how the service should be started and stopped.
ExecStart: The command to start the service.
Restart: Configures how systemd should handle service restarts.
always: Tells systemd to always restart the service if it stops.
[Install]: This section contains information about how the service should be installed and started.
WantedBy: Configures what target the service should be started at.
multi-user.target: Starts the service when the system is in multi-user mode.
π Congratulations, you have successfully created and managed a systemd service on your Linux system!