2023-06-26 13:32:02 +01:00
|
|
|
---
|
|
|
|
title: How to create a Systemd service
|
|
|
|
description: How to create a Systemd service on Ubuntu 22.04
|
|
|
|
published: true
|
|
|
|
date: 2023-06-26T13:28:49.025Z
|
|
|
|
tags: docker, ubuntu, installation, containerization, container, linux, server, ubuntu, systemd, service
|
|
|
|
editor: markdown
|
|
|
|
dateCreated: 2023-06-26T13:28:49.025Z
|
|
|
|
---
|
|
|
|
|
|
|
|
# How to create a Systemd service on Ubuntu 22.04
|
|
|
|
|
|
|
|
This guide provides step-by-step instructions for creating a Systemd service on Ubuntu, specifically tested on version 22.04.
|
|
|
|
|
|
|
|
## Prerequisites
|
|
|
|
|
|
|
|
- A system running Ubuntu 22.04
|
|
|
|
- A user account with sudo privileges
|
|
|
|
- An internet connection
|
|
|
|
|
|
|
|
## Get Started
|
|
|
|
|
|
|
|
- Begin by creating a new file for your service unit. Open a terminal and run the following command:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo nano /etc/systemd/system/my-service.service
|
|
|
|
```
|
|
|
|
|
|
|
|
This command will open a new file in the nano text editor. You can replace `my-service` with the name of your service.
|
|
|
|
|
|
|
|
- Next, you need to define the service unit. Add the following lines to the file:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
[Unit]
|
|
|
|
Description=<name>
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
ExecStart=<command>
|
|
|
|
WorkingDirectory=<directory>
|
|
|
|
Restart=always
|
2023-06-27 08:48:02 +01:00
|
|
|
User=root (use a non root user for better security)
|
2023-06-26 13:32:02 +01:00
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
```
|
|
|
|
|
|
|
|
- Now we can enable the service to run on startup. Run the following command in the terminal:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo systemctl enable my-service
|
|
|
|
```
|
|
|
|
|
|
|
|
- Finally, you can start the service by running the following command:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo systemctl start my-service
|
|
|
|
```
|
|
|
|
|
|
|
|
- You can check the status of the service by running the following command:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo systemctl status my-service
|
|
|
|
```
|
|
|
|
|
|
|
|
## Finished
|
|
|
|
|
|
|
|
You have successfully created a Systemd service on Ubuntu 22.04. You can now manage the service using the `systemctl` command.
|
|
|
|
|
|
|
|
## Logs
|
|
|
|
|
|
|
|
You can get logs from the service by running the following command:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sudo journalctl -u my-service
|
|
|
|
```
|