How to Create a Systemd Service in Linux?

Think of Systemd Service as the brain behind how your Linux system runs. It ensures that when you turn on your computer, everything starts in the right order and keeps running smoothly.

Systemd manages different kind of services, which are ultimately the programs or processes that runs in the background.

Examples of services include:

For Example: An Nginx web server running on port 443 and serving traffic. Now Suppose if the server got rebooted due to some underlying OS related issues, but when it comes up, we eventaully want our service also to be UP and Running without any manual intervnetion. Hence, for this we can run a service as a systemd process, so that it eventually automatically comes UP.

This is one of the use cases for the Systemd.

How to Create a Systemd Service in Linux?

Why Do We Need Systemd?

Startup Management:

When you press the power button, your computer doesn’t start automatically. Systemd makes sure things start in the correct order.

For example:

First, mount the disk so the system can read files.

Next, start the network services so you can connect to the internet.

Finally, launch the desktop environment or other user applications.

Keeps the System Running:

Just Imagine your nginx web server crashes, without Systemd, you might have to restart it manually.

With Systemd, we can set the services in such a way that it observes the crash and restarts the server automatically.

Easy Control:

Systemd provides simple commands to manage services. For example, you can start, stop, or restart a service using just one command.

How to create a Systemd Service?

Write the Service File

Service files define and tells how your application or script will be managed. They are typically stored in the path /etc/systemd/system/.

  1. Open a terminal and create a service file:
    sudo vim /etc/systemd/system/my-custom-service.service
  2. Populate the file with the following structure:
    [Unit]
    Description=<Add Description for your service>
    After=network.target

    [Service]
    ExecStart=</path/to/your/script>
    Restart=on-failure
    User=<USER>
    Group=<GROUP>
    Environment="ENV_VAR_NAME=value"

    Restart=always
    RestartSec=3


    [Install]
    WantedBy=multi-user.target
  3. Now, Reload Systemd and Start the Service.
    Once the service file is created, reload Systemd to recognize it:
    sudo systemctl daemon-reload
  4. Start the service:
    sudo systemctl start my-custom-service
  5. To Enable it to run at boot:
    sudo systemctl enable my-custom-service
  6. To disable your service on every reboot
    sudo systemctl disable my-custom-service
  7. To check the status of your service.
    sudo systemctl staus my-custom-service

Common Flags:

  1. ExecStart: This is the command which will be used to execute your custom script.
  2. Restart: This flag defines the restart policies. Common values are:
    no: Do not restart.
    on-failure: Restart if the service fails.
    always: Restart no matter the exit code.
  3. User: It defines the user which will be used to execute this script. It is recommended to create a dedicated custom system user with appropriate permissions.
  4. Group: It is used to define the group under which the user is added.
  5. Environment: This is used to pass any custom ENV variables to the script.

The above configuration file defines a service that:

  • Starts after the network is ready.
  • Runs a program defined at ExecStart.
  • Automatically restarts if it fails and the time it needs to wait for restart is 3s.
  • Runs as user-defined under <USER> and is started during the multi-user target (normal boot).

How They Work Together

In the Service file, there are multiple parameters supported like:

  • [Unit]: specifies the what and when. It provides metadata and dependencies which are needed to successfully run this service.
  • [Service]: specifies the how. It defines the behavior and the commands like what it should run, how it runs, and what happens if it fails.
  • [Install]: specifies the where. It determines when and how the service is started during startup/boot.

🚩 Our Recent Posts

Share

Leave a Comment