Adding your own systemd service in Linux

Published: January 9, 2022

You will learn how to add a service in Linux. You will learn how to add a service through the systemd daemon using the systemctl command, and how to configure the service to start when Linux boots. You will need administrator privileges.

Linux servers are designed to run continuously. To make this possible, they need to handle many complex tasks required for the system to work. Services are used for this purpose, allowing specific system functions to run. Services can run in the background, and properly configured services are started when the system boots. You can add your own services that will run tasks in the background.

Adding a service

Go to the directory used for service configuration files.

cd /etc/systemd/system

Create a configuration file for your service with a name ending in .service, for example:

sudo nano <service_name>.service

Add the following content to the file and adjust it to your needs:

[Unit]
Description=<service_description>

[Service]
User=<user e.g. root>
WorkingDirectory=<script_directory e.g. /root>
ExecStart=<absolute_script_path>
Restart=always

[Install]
WantedBy=multi-user.target

Save the file and exit the editor. Reload the service files so that the new service is picked up.

sudo systemctl daemon-reload

Then start the service.

sudo systemctl start <service_name>.service

To check the status of your service, enter:

sudo systemctl status <service_name>.service

To enable the service so that it starts after every reboot, enter:

sudo systemctl enable <service_name>.service

To disable the service so that it does not start after every reboot, enter:

sudo systemctl disable <service_name>.service

Comments (0)

No comments yet.

Add a comment

Comments are published after moderation. Your e-mail address stays private.