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: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:
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 (ExecStartPreandExecStartPost), and a restart policy (Restart=always). - [Install]: Configures the service to begin when the system reaches the multi-user target during the boot sequence.
Example: The Docker Service
For a more complex example, consider the Docker daemon. After installing Docker, an executable nameddocker 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.
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.