svg
Post Image
By Daniel Tanque12 de Outubro, 2023In BashDevOpsLearning

Service Unit Files

You may have notice that when you boot your PC you have certain services or tasks that automatically start, can be wi-fi, the antivirus and many others. Those are underlining services and it’s possible to define such for your applications, this is even more important on servers, sometimes they have to reboot and you want them to start automatically.

In this article you will learn how to perform those operations automatically by using Service Unit Files.

What are Service Unit Files?

Service unit files, in the context of Linux systemd, are configuration files that define and manage system services or daemons. systemd is a system and service manager that has become the standard initialization system in many Linux distributions.

Service unit files typically have a .service file extension and are written in a declarative format that specifies how a service should be started, stopped, and managed by systemd. These files are located in directories like /etc/systemd/system/ or /lib/systemd/system/, and they provide systemd with the necessary information to control a service.

What’s their structure?

The main components of a service unit file is:

  1. Unit Section – This section defines the basic information about the service, including its name, description, and dependencies.
  2. Service Section – This section specifies how the service should be started and stopped. You can define the executable to run, startup type, user, and more.
  3. Install Section – This section defines how the service should be enabled and started at boot time

Here is some example:

Unit Section

[Unit]
Description=My Sample Service
After=network.target

Service Section

[Service]
ExecStart=/usr/bin/my-service
Type=simple
User=myuser
Restart=on-failure

Install Section

[Install]
WantedBy=multi-user.target

How to run systemctl

After being setup the new service it’s as simple as running the following commands:

service httpd start || systemctl stop httpd
systemctl start httpd
systemctl statuts httpd
systemctl enable httpd

Conclusion

This is how you can setup your services so it gets easier to manage services, mainly your apps in servers and you want them to initialize automatically.

svgPackage Manager
svg
svgBasics of Networking: Switching and Routing

Leave a reply