docs/guides/ubuntu/pages/22_04/systemd-service.md

1.7 KiB

title description published date tags editor dateCreated
How to create a Systemd service How to create a Systemd service on Ubuntu 22.04 true 2023-06-26T13:28:49.025Z linux, server, ubuntu, systemd, service markdown 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:
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:
[Unit]
Description=<name>

[Service]
ExecStart=<command>
WorkingDirectory=<directory>
Restart=always
User=root (use a non root user for better security)

[Install]
WantedBy=multi-user.target
  • Now we can enable the service to run on startup. Run the following command in the terminal:
sudo systemctl enable my-service
  • Finally, you can start the service by running the following command:
sudo systemctl start my-service
  • You can check the status of the service by running the following command:
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:

sudo journalctl -u my-service