Skip to main content
Linux services are background processes that are essential for running software such as web servers, database servers, and DevOps tools like Docker. They ensure critical applications continue running after a server restart, start in the correct order when multiple services are present, and manage dependencies effectively. When you install a software package that runs in the background, it is automatically set up as a service. For example, installing the Apache web server creates an HTTPD service. You can manage it using the legacy service command or the modern systemctl command:
Both commands achieve the same goal, but it is recommended to use systemctl for managing services on modern Linux distributions.

Managing Services

Start the HTTPD service with the legacy command:
Or, use the preferred systemctl command:

Configuring a Custom Application as a Service

Imagine you have a simple Python-based web server located at /opt/code/my_app.py. When executed with the Python interpreter, it starts a web server listening on port 5000. Running the command:
produces output similar to this:
You can verify that the server is working by issuing:
which should return:
By configuring your Python application as a service, you abstract away the need to manually handle the command line and file paths each time. Start and stop the service simply with:
This approach simplifies service management and minimizes manual intervention.

Creating a Systemd Unit File

To ensure your application starts automatically on boot or restarts if it crashes, you need to create a systemd unit file. These files are typically stored in /etc/systemd/system. Create a file named my_app.service with the following content:

Explanation of the Unit File Sections

  • [Unit]: Provides metadata and a short description of the service.
  • [Service]: Contains the command to start your application (ExecStart), optional commands that run before or after starting the service (ExecStartPre and ExecStartPost), and a restart policy (Restart=always).
  • [Install]: Configures the service to begin when the system reaches the multi-user target during the boot sequence.
After saving the file, reload systemd to register your new service:
Then, start the service:
Check its status using:
A successful status output might look like:
Test the service again with:
which should return:
This configuration ensures that your service starts automatically on boot and restarts if it crashes.

Example: The Docker Service

For a more complex example, consider the Docker daemon. After installing Docker, an executable named docker is available at /usr/bin/docker, and it is configured as a service using a unit file typically located at /lib/systemd/system/docker.service. Below is a typical Docker service configuration:

Breakdown of Docker’s Service Unit File

  • [Unit]: Sets a description and lists dependency requirements.
  • [Service]: Defines how Docker is started, how to reload its configuration, and its restart behavior. It also sets resource limits.
  • [Install]: Specifies that Docker should start when the system reaches the multi-user target.
After installation, the Docker daemon automatically runs in the background and listens for Docker commands. This configuration serves as a model for setting up any custom application as a service.
In practice, applying these configurations allows you to efficiently manage services on your Linux system, ensuring reliability and ease of maintenance. For more details on managing services, consider exploring resources such as Kubernetes Basics and the Docker Documentation.

Watch Video

Practice Lab